1

I used the ocr sample in this link https://github.com/rmtheis/android-ocr Every thing is working fine but i want it in Portrait view,I followed the steps in this link , Zxing Camera in Portrait mode on Android, to enable ocr tesstow in Portrait mode . The View is portrait now but the camera is still taking the picture in landscape mode.

Any help ? enter image description here

enter image description here

  final class PreviewCallback implements Camera.PreviewCallback {

  private static final String TAG = PreviewCallback.class.getSimpleName();

  private final CameraConfigurationManager configManager;
  private Handler previewHandler;
  private int previewMessage;

  PreviewCallback(CameraConfigurationManager configManager) {
  this.configManager = configManager;
  }

  void setHandler(Handler previewHandler, int previewMessage) {
  this.previewHandler = previewHandler;
  this.previewMessage = previewMessage;
  }

 // (NV21) format.
 @Override
  public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  Handler thePreviewHandler = previewHandler;
  if (cameraResolution != null && thePreviewHandler != null) {
  Message message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,
      cameraResolution.y, data);
   message.sendToTarget();
   previewHandler = null;
   } else {
    Log.d(TAG, "Got preview callback, but no handler or resolution available");
   }
   }
Community
  • 1
  • 1
Reham
  • 1,916
  • 6
  • 21
  • 45

2 Answers2

1

Are you using the preview data with this method:

public void onPreviewFrame(byte[] data, Camera camera) {}

If yes, then I can help you, since I am doing very similar project (that will be open sourced soon)

here is the code that I am using to rotate the preview image

public static Bitmap getBitmapImageFromYUV(byte[] data, int width,
        int height, int degree, Rect rect) {
    Bitmap bitmap = getBitmapImageFromYUV(data, width, height, rect);
    return rotateBitmap(bitmap, degree,rect);

}

public static Bitmap rotateBitmap(Bitmap source, float angle, Rect rect) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    source = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
    source = Bitmap.createBitmap(source, rect.left, rect.top, rect.width(), rect.height());

    if(mShouldSavePreview)
        saveBitmap(source);
    return source;

}

public static Bitmap getBitmapImageFromYUV(byte[] data, int width,
        int height, Rect rect) {
    YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width, height,
            null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(0, 0, width, height), 90, baos);

    byte[] jdata = baos.toByteArray();
    BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
    bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length,
            bitmapFatoryOptions);

    Log.d(TAG,"getBitmapImageFromYUV w:"+bmp.getWidth()+" h:"+bmp.getHeight());


    return bmp;
}
MP23
  • 1,763
  • 20
  • 25
  • Yes,but where should i add this code . i added PreviewCallback Class to the above code. Can you help me with it – Reham Mar 02 '14 at 13:30
  • Check that part is my source here: https://gist.github.com/mplackowski/4be81b0d4c658db79544 from the preview callback you need to grab the byte[] array and use this method 'getBitmapImageFromYUV(byte[] data, int width, int height, int degree, Rect rect)' retrived bitmap goes to the Tesseract object (I am using tess-two) – MP23 Mar 02 '14 at 13:35
  • According to you implementation, you need to put that code in the place where the data[] array is converted to bitmap, you are sending it with the Message, but I have no idea where it is converted in your code – MP23 Mar 02 '14 at 14:12
  • Thank you , but the code which i'm trying with , is complex .Until now i did not find a way to use your code with it. – Reham Mar 02 '14 at 14:55
  • Ok, I understand that, but you for sure are able to find the place where the data array is converted to Bitmap. It is in landscape so you need to rotate it before you get the final bitmap for further processing – MP23 Mar 02 '14 at 15:01
  • You can check my code here: https://github.com/mplackowski/previewOCR but first of all you should show the part of your code where you obtain the image bitmap from byte array – MP23 Mar 06 '14 at 14:29
  • I guess the solution will start from this class , https://github.com/rmtheis/android-ocr/blob/master/android/src/edu/sfsu/cs/orange/ocr/PlanarYUVLuminanceSource.java – Reham Mar 09 '14 at 11:05
  • @Rehan, cud u solve the problem, cos I run into the same issue. – Bubunyo Nyavor Aug 09 '14 at 08:11
0

guys i found the solution!

Replace the next code in function: ocrDecode(byte[] data, int width, int height) in DecodeHandler.java file

    beepManager.playBeepSoundAndVibrate();
    activity.displayProgressDialog();

    // *************SHARNOUBY CODE
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++)
            rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width;
    width = height;
    height = tmp;
    //******************************
    // Launch OCR asynchronously, so we get the dialog box displayed
    // immediately
    new OcrRecognizeAsyncTask(activity, baseApi, rotatedData, width, height)
        .execute();

...the problem was in the switch case in the function handleMessage(Message message) the second case was never triggered which calls the rotation code