2014-03-02 118 views
1

我在此鏈接中使用了ocr示例https://github.com/rmtheis/android-ocr 每件事情都很好,但我希望它在縱向視圖中,我按照此鏈接中的步驟Zxing Camera in Portrait mode on Android啓用肖像模式中的ocr tesstow 。 View現在是肖像,但照相機仍然以橫向模式拍攝照片。Android-Ocr在肖像中使用Tesseract

任何幫助? 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"); 
    } 
    } 

回答

1

您使用的是預覽數據以這種方法:

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

如果是的話,我可以幫你,因爲我做的非常類似的項目(將即將開源)

這裏是我用來旋轉預覽圖像的代碼

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

是的,但我應該在哪裏添加此代碼。我在上面的代碼中添加了PreviewCallback類。你能幫我嗎 – Reham

+0

檢查部分是我的來源:預覽回調https://gist.github.com/mplackowski/4be81b0d4c658db79544你需要抓住byte []數組,並使用此方法'getBitmapImageFromYUV(byte [ ] data,int width, int height,int degree,Rect rect)'retrived bitmap goes to Tesseract object(我正在使用tess-2) – MP23

+0

根據你的實現,你需要把該代碼放在數據的地方[]數組被轉換爲位圖,您使用消息發送它,但我不知道它在代碼中的轉換位置 – MP23

0

夥計們,我找到了解決方案!

替換功能的下一個代碼:ocrDecode(字節[]數據,INT寬度,INT高度)DecodeHandler.java文件

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(); 

...問題是在開關case在函數handleMessage(消息消息) 第二種情況從未觸發,它調用旋轉代碼