To facilitate smooth integration, Rekor CarCheck® offers a range of code samples in different programming languages. These samples serve as practical guides, demonstrating how to interact with the API effectively. Whether you are working with Bash, Python, C#, Java, or JavaScript, you will find detailed examples below to help you understand the implementation process.
#!/usr/bin/pythonimport requestsimport base64import json# Sample image file is available at http://plates.openalpr.com/ea7the.jpgIMAGE_PATH ='/tmp/sample.jpg'SECRET_KEY ='sk_DEMODEMODEMODEMODEMODEMO'withopen(IMAGE_PATH, 'rb')as image_file: img_base64 = base64.b64encode(image_file.read())url ='https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s'% (SECRET_KEY)r = requests.post(url, data = img_base64)print(json.dumps(r.json(), indent=2))
C#
Java
JavaScript
Results
Upon successful use of the API, the JSON response will provide detailed information in a structured format. The response includes key data points such as license plate number, vehicle make and model, color, and additional relevant information, ensuring a comprehensive overview of the recognized vehicle.
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.IO;
namespace ConsoleApplicationTest
{
class Program
{
private static readonly HttpClient client = new HttpClient();
public static async Task<string> ProcessImage(string image_path)
{
string SECRET_KEY = "sk_DEMODEMODEMODEMODEMODEMO";
Byte[] bytes = File.ReadAllBytes(image_path);
string imagebase64 = Convert.ToBase64String(bytes);
var content = new StringContent(imagebase64);
var response = await client.PostAsync("https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=" + SECRET_KEY, content).ConfigureAwait(false);
var buffer = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var byteArray = buffer.ToArray();
var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
return responseString;
}
static void Main(string[] args)
{
Task<string> recognizeTask = Task.Run(() => ProcessImage(@"C:\Temp\car1.jpg"));
recognizeTask.Wait();
string task_result = recognizeTask.Result;
System.Console.WriteLine(task_result);
}
}
}
import java.net.;
import java.io.;
import java.nio.file.*;
import java.util.Base64;
class TestOpenALPR {
public static void main(String[] args)
{
try
{
String secret_key = "sk_DEMODEMODEMODEMODEMODEMO";
// Read image file to byte array
Path path = Paths.get("/storage/projects/alpr/samples/testing/car1.jpg");
byte[] data = Files.readAllBytes(path);
// Encode file bytes to base64
byte[] encoded = Base64.getEncoder().encode(data);
// Setup the HTTPS connection to api.openalpr.com
URL url = new URL("https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=" + secret_key);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection)con;
http.setRequestMethod("POST"); // PUT is another valid option
http.setFixedLengthStreamingMode(encoded.length);
http.setDoOutput(true);
// Send our Base64 content over the stream
try(OutputStream os = http.getOutputStream()) {
os.write(encoded);
}
int status_code = http.getResponseCode();
if (status_code == 200)
{
// Read the response
BufferedReader in = new BufferedReader(new InputStreamReader(
http.getInputStream()));
String json_content = "";
String inputLine;
while ((inputLine = in.readLine()) != null)
json_content += inputLine;
in.close();
System.out.println(json_content);
}
else
{
System.out.println("Got non-200 response: " + status_code);
}
}
catch (MalformedURLException e)
{
System.out.println("Bad URL");
}
catch (IOException e)
{
System.out.println("Failed to open connection");
}
}
}
<html>
<title>
CarCheck API Demo
</title>
<head>
<script>
// Open connection to api.openalpr.com
var secret_key = "sk_DEMODEMODEMODEMODEMODEMO";
var url = "https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=" + secret_key;
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
// Send POST data and display response
xhr.send("base64_string"); // Replace with base64 string of an actual image
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
document.getElementById("response").innerHTML = xhr.responseText;
} else {
document.getElementById("response").innerHTML = "Waiting on response...";
}
}
</script>
</head>
<body>
JSON response: <p id="response"></p><br>
</body>
</html>