Skip to content

Instantly share code, notes, and snippets.

@danleyb2
Created August 26, 2021 14:04
Show Gist options
  • Save danleyb2/baed884d837aa42298c1786c04908e33 to your computer and use it in GitHub Desktop.
Save danleyb2/baed884d837aa42298c1786c04908e33 to your computer and use it in GitHub Desktop.
Use in-memory image in Plate Recognizer
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class PlaterecognizerLookupMemory {
static final String charset = "UTF-8";
static final String PLATERECOGNIZER_API_TOKEN = "08b1a7048080dfef0bf######################";
public static void main(String[] args) {
try {
URL url = new URL("https://app.platerecognizer.com/static/demo.jpg");
sendMemoryFile(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void sendMemoryFile(URL url) throws Exception {
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
String fileName = "snapshot.jpg";
try (InputStream urlInputStream = url.openStream()) {
try {
URL obj = new URL("https://api.platerecognizer.com/v1/plate-reader/");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Token " + PLATERECOGNIZER_API_TOKEN);
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
con.setRequestProperty("Accept", "application/json");
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
// os.write(POST_PARAMS.getBytes());
PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, charset), true);
// Send binary file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"upload\"; filename=\"" + fileName + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
byte[] buffer = new byte[1024];
int len;
while ((len = urlInputStream.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF).flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("POST failed");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment