1

I've successfully modified ZXing lib to portrait mode following this post, also modify the preview rect to an square like img below. But I found the actually scanning rect is only half size of preview rect, only when I aligned qrcode to upper half of preview rect then I can get a result quick and smooth, just like ZXing demo app.

I check both getFramingRect() & getFramingRectInPreview() method in CameraManager to modify the preview rect, and from ViewFinderView.onDraw() I can print out those 2 rect. But I still don't know which rect is actually for DETECTING. Is it frame or previewFrame? Or something else? How could I align my preview square rect to the detecting rect???

@Override
public void onDraw(Canvas canvas) {
    if (cameraManager == null) {
        return; // not ready yet, early draw before done configuring
    }
    Rect frame = cameraManager.getFramingRect();
    Rect previewFrame = cameraManager.getFramingRectInPreview();
    if (frame == null || previewFrame == null) {
        return;
    }
    int width = canvas.getWidth();
    int height = canvas.getHeight();

    Log.d(TAG, "@ FramingRect : " + frame.left + "/" + frame.top + "/" + frame.right + "/" + frame.bottom);
    Log.d(TAG, "@ Preview FramingRect : " +
            previewFrame.left + "/" + previewFrame.top + "/" + previewFrame.right + "/" + previewFrame.bottom);

enter image description here

Whenever I adjust the previewRect in the getFramingRectInPreview() this exception will occur:

java.lang.IllegalArgumentException: Crop rectangle does not fit within image data.
    at com.google.zxing.PlanarYUVLuminanceSource.<init>(PlanarYUVLuminanceSource.java:50)
    at com.google.zxing.client.android.camera.CameraManager.buildLuminanceSource(CameraManager.java:339)
    at com.google.zxing.client.android.DecodeHandler.decode(DecodeHandler.java:87)
    at com.google.zxing.client.android.DecodeHandler.handleMessage(DecodeHandler.java:55)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at com.google.zxing.client.android.DecodeThread.run(DecodeThread.java:104)

After try&error I can only move previewRect down by 100, move right by 60. It seems make detecting process more smooth.

Community
  • 1
  • 1
Robert
  • 1,660
  • 22
  • 39

1 Answers1

1

getFramingRect() is relative to the screen, and getFramingRectInPreview() is relative to the preview image data from the camera. They're calculated to be proportionally the same fraction of width and height, but if you've modified the code then maybe this part wasn't correctly modified.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • 1
    Yes, I found `getFramingRect()` & `getFramingRectInPreview()` are returning a proportionally resemble. Here I even draw both of them on the screen. I found them ain't aligned. Being Curious so I also draw both of these rect in the original ZXing Android project, these ain't aligned too! So do I need to align them together? – Robert Jul 13 '13 at 14:25
  • here are my two rect, and those two rect in zxing android.(green rect is the previewFramingRect) http://d.pr/i/eCjN http://d.pr/i/U4DB – Robert Jul 13 '13 at 14:43
  • @Sean Owen Which method I should override to change the scanning area.I override scanning area in getFramingRectInPreview(),but it only updates view not scan inside rectangle in Nexus 5x(7.1.1). http://stackoverflow.com/questions/43973913/zxing-viewfinderview-view-updates-but-scanning-area-not-updated-on-nexus-5x – Ramprasad May 16 '17 at 06:13