Unable to recognize image format every time I request

Hello OCRns ,I m new in ocr TECH. Since in java dev I was testing ocr api free in postman and in my java app .I have copied and paste exact code as describe from ocr docs and I m getting 403 in postman .even if I have used api key which was provided by your mail service but still can’t figure out what to do…my ssl certs are also valid.since I’m using self signed certs.plz help me through it thanks .
and now after resolving 403 now im getting this.

-----{“OCRExitCode”:99,“IsErroredOnProcessing”:true,“ErrorMessage”:[“Unable to recognize the file type”],“ProcessingTimeInMilliseconds”:“15”}.

i m sending base64string image as my simple image file.

THIS IS MY CODE:
ByteArrayOutputStream os=new ByteArrayOutputStream();
ImageIO.write(i,“png”,os);
byte l=os.toByteArray();
URL u=new URL(“http://api.ocr.space/parse/image”);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
con.setRequestMethod(“POST”);
con.setRequestProperty(“User-Agent”, “Mozilla/5.0”);
con.setRequestProperty(“apikey”,“”);
String im=Base64.getEncoder().encodeToString(l);

  JSONObject  postDataParams=new JSONObject();
   postDataParams.put("isOverlayRequired", true);
      postDataParams.put("detectOrientation", true);
      postDataParams.put("detectOrientation", true);
      postDataParams.put("base64Image",im );
      postDataParams.put("language", "eng");
      postDataParams.put("OCREngine", 1);
1 Like

Can you please a screenshot of your Postman call? Then we can test it here.

Here are a few test BASE64 strings you can use:

All works fine for me. For example, this is the call with the PNG image in base64:

here my screenshot for my postman request that im sending with post.is everything okay?
what other 9 headers you are using to get response from server?
in my java application via using httpsURLConnection .i m always getting “unable to recognized file format” what to do ?

In your screenshot it seems you have no internet connection to the API, or? It says it can not reach the https://api.ocr.space/ endpoint.

internet is up everthing is running fine .what to do now?i m working on it from three days .done everything i ve also decided to go back to tessarect ocr but it does not provide accurate result…plz help me .
this is the response i m getting into my spring boot app while using httpurlconnection POST request with all headers in their place.
-----{“OCRExitCode”:99,“IsErroredOnProcessing”:true,“ErrorMessage”:[“Unable to recognize the file type”],“ProcessingTimeInMilliseconds”:“1”}
But my GET request is working fine but it required http url of an image resource which is not a suited requirement for my project.

Can you please post the Postman screenshot when you are getting this error response?

i m using spring boot to post base64 image format: heres my code snippet below:

 ByteArrayOutputStream os=new ByteArrayOutputStream();
	  
	  ImageIO.write(i,"png",os);

//STORING IMAGE STREAM INTO BASE64 FORMAT and wraping into output stream
byte l=os.toByteArray();
URL u =new URL(“https://api.ocr.space/parse/image”);
HttpsURLConnection con = (HttpsURLConnection) u.openConnection();
String im=Base64.getEncoder().encodeToString(l);
//add request header
System.out.println(im);
con.setRequestMethod(“POST”);
con.setRequestProperty(“User-Agent”, “Mozilla/5.0”);
con.setRequestProperty(“apikey”,“f30145498288957”);
con.setRequestProperty(“Content-type”,“application/json”);
con.setRequestProperty(“Accept-Language”, “en-US,en;q=0.5”);
JSONObject postDataParams = new JSONObject();
postDataParams.put(“isOverlayRequired”, true);
postDataParams.put(“detectOrientation”, true);
postDataParams.put(“detectOrientation”, true);
postDataParams.put(“base64Image”,“iVBORw0KGgoAAAANSUhEUgAAAPoAAADcCA IAAADWeytBAACAAElEQVR4Xuy9d3AcR57viZFGjqREUhQ…more data here but to long for describing here”);
postDataParams.put(“language”, “eng”);
postDataParams.put(“OCREngine”, 1);

      // Send post request
      con.setDoOutput(true);
     DataOutputStream wr = new DataOutputStream(con.getOutputStream());
     System.out.println("Utf-8:"+postDataParams.toString().getBytes("UTF-8"));
      wr.writeBytes(postDataParams.toString());
      wr.flush();
      wr.close();

     //inputstream fetching code below con.getinputstream() code
    and whatever i do i m getting this error:
       {“OCRExitCode”:99,“IsErroredOnProcessing”:true,“ErrorMessage”:[“Unable to recognize the file type”],“ProcessingTimeInMilliseconds”:“1”}

what can i do now? please suggest some

Hi, try my solution here;

Hope it helps.

Thanks,
Zabir.

Greetings,

I’m also facing this error while uploading a base64 image. It works perfectly using Postman, but when I try it with Axios, it throws this error (Unable to recognize the file type). Here’s the code:

const ocrRequest = ({ base64, fileType }) => {
    // TODO: env variable
    const url = "https://api.ocr.space/parse/image";
    const fd  = new FormData();

    fd.append("apikey", "<key>");
    fd.append("base64Image", base64);
    fd.append("filetype", fileType); // variable with "JPG" value 
    fd.append("OCREngine", 2);

    return axios({
      method : "POST",
      url         : url,
      headers: {
        "Content-Type": "multipart/form-data",
      },
      data     : fd,
    });
  };

@ivan-marquez If you post a URL then it works?

I haven’t tried with url. I’ll get back with the results. Thanks.

Thankyou So much it worked!

base64image doesnot work sometimes but sometimes it does… i have used multipartbuilder java api to send file in inputstream with httpclient working fine now.
@PostMapping(“/ImageTest”)
public String imageReader(@RequestParam(“images”) MultipartFile mf) throws IOException {
System.out.println(mf.getOriginalFilename());
HttpClient hcl=HttpClients.createDefault();
HttpPost h=new HttpPost(“http://api.ocr.space/parse/image”);
File file = new File(mf.getOriginalFilename());
// FileBody fileBody = new FileBody(file, ContentType.IMAGE_PNG);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
StringBody sb=new StringBody(new String(mf.getBytes()),ContentType.IMAGE_PNG);
builder.setMode(HttpMultipartMode.STRICT);
//Creating form body
builder.addBinaryBody(“file”, mf.getInputStream(),ContentType.IMAGE_PNG,mf.getOriginalFilename()); //IMAGE AS INPUT STREAM WITH ITS TYPE
builder.addTextBody(“language”, “eng”); //LANGUAGE TO BE PARSED IN OCR
builder.addTextBody(“OCREngine”, “2”); //OCRENGINE 1 OR 2
builder.addTextBody(“filetype”,“PNG”); //FILETYPE .PNG,.JPG .TIF ETC
builder.addTextBody(“scale”,“true”); //SCALE ZOOMING IMAGE
builder.addTextBody(“detectOrientation”, “true”); //ORIENTATION OF IMAGE
builder.addTextBody(“isOverlayRequired”, “true”);
h.addHeader(“apikey”,“f30145498288957” ); //SECRET API KEY
org.apache.http.HttpEntity hc= builder.build(); //GETTING HTTPENTITY OF THIS MUTLIPART BODY
h.setEntity(hc);
HttpResponse res= hcl.execute(h);
String result= EntityUtils.toString(res.getEntity());
System.out.println( result);//successfulll now its time to plan
Gson g=new Gson();
Result re=g.fromJson(result,Result.class);
System.out.println(re.getParsedResult()[0].getTextOverlay().Lines[0].getLineText());
System.out.println(resultFilter(re.getParsedResult()[0].getTextOverlay().Lines[0].getLineText()));
return “Done!”;
}
TRY THIS.

If it does not work, then does the same base64 string work in postman?