Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Last active June 16, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhaniksahni/cc70a9345dbbef5b243325545f822cd4 to your computer and use it in GitHub Desktop.
Save dhaniksahni/cc70a9345dbbef5b243325545f822cd4 to your computer and use it in GitHub Desktop.
License Plate Number Reader in Salesforce
public class CaseService {
public static void UpdatePlateNumber(string record,string plate)
{
try
{
Case caseRecord=[Select Id, Plate_Number__c from Case where id=:record];
caseRecord.Plate_Number__c=plate;
update caseRecord;
}
catch(Exception e)
{
system.debug(e);
}
}
}
public class LicensePlateReader {
static string apiUrl='https://macgyverapi-license-plate-recognition-v1.p.rapidapi.com';
static string token='*******************************'; // Your token here
@InvocableMethod
public static void getPlateNumber(List<string> records)
{
List<Id> recodId = new List<Id>();
List<Case> cases = [SELECT Id FROM Case WHERE Id in :records];
for (Case account : cases) {
recodId.add(account.Id);
}
List<ContentDocumentLink> links=[SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=:recodId];
Set<Id> ids=new Set<Id>();
for(ContentDocumentLink link:links)
{
ids.add(link.ContentDocumentId);
}
List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
List<ContentDistribution> cdts=[select ContentDocumentId,DistributionPublicUrl,ContentDownloadURL from ContentDistribution where ContentDocumentId =:ids];
Map<String, String> contentList= new Map <String, String>();
for(ContentDistribution cdt:cdts)
{
contentList.put(cdt.ContentDocumentId, cdt.ContentDownloadURL);
}
for(ContentVersion attach:versions)
{
PlateReaderRequest request=new PlateReaderRequest();
request.id='3X5D3d2k';
request.key='free';
PlateReaderRequest.RequestImage data=new PlateReaderRequest.RequestImage();
data.country='us';
if(cdts.size()>0)
{
String imageUrl=contentList.get(attach.ContentDocumentId);
system.debug('imageUrl'+imageUrl);
if(string.isNotBlank(imageUrl))
{
data.image_url=imageUrl;
data.numberCandidates=2;
request.data=data;
string requestBody=JSON.serialize(request);
string response=callAPI(requestBody);
PlateNumberResponse plateRespone=PlateNumberResponse.parse(response);
if(plateRespone!=null && plateRespone.results!=null)
{
PlateNumberResponse.Result result=plateRespone.results[0];
if(result.Candidates!=null)
{
PlateNumberResponse.Candidate candidate=result.Candidates[0];
if(candidate!=null)
{
CaseService.UpdatePlateNumber(recodId[0],candidate.plate);
}
}
}
}
}
}
}
private static string callAPI(string body)
{
Http h = new Http();
HttpRequest hr = new HttpRequest();
hr.setHeader('x-rapidapi-host', 'macgyverapi-license-plate-recognition-v1.p.rapidapi.com');
hr.setHeader('x-rapidapi-key', '*****************************************'); //uppdate your key from step 1
hr.setHeader('Content-type', 'application/json');
hr.setHeader('accept', 'application/json');
hr.setHeader('useQueryString', 'true');
hr.setTimeout(60000);
hr.setEndpoint(apiUrl);
hr.setMethod('POST');
hr.setBody(body);
HttpResponse r = h.send(hr);
return r.getBody();
}
}
public class PlateNumberResponse{
public Decimal processing_time_ms{get;set;}
public Integer img_width{get;set;}
public Integer img_height{get;set;}
public Decimal epoch_time{get;set;}
public list<String> regions_of_interest{get;set;}
public String data_type{get;set;}
public list<Result> results{get;set;}
public Integer version{get;set;}
public class Result{
public String region{get;set;}
public Integer region_confidence{get;set;}
public Integer plate_index{get;set;}
public Decimal processing_time_ms{get;set;}
public Integer matches_template{get;set;}
public Integer requested_topn{get;set;}
public Decimal confidence{get;set;}
public list<Coordinate> coordinates{get;set;}
public list<Candidate> candidates{get;set;}
public String plate{get;set;}
}
public class Coordinate{
public Integer x{get;set;}
public Integer y{get;set;}
}
public class Candidate{
public Decimal confidence{get;set;}
public Integer matches_template{get;set;}
public String plate{get;set;}
}
public static PlateNumberResponse parse(String json) {
return (PlateNumberResponse) System.JSON.deserialize(json, PlateNumberResponse.class);
}
}
public class PlateReaderRequest
{
public string id{get;set;}
public string key{get;set;}
public RequestImage data{get;set;}
public class RequestImage
{
public string image_url{get;set;}
public string country{get;set;}
public integer numberCandidates{get;set;}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment