Question

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");
   }
   }

No correct solution

OTHER TIPS

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;
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top