Android Zxing 轉換豎屏掃描且提高識別率


  最近的一個Android需要用到掃碼功能,用的是Zxing開源庫。Zxing的集成就不說了,但是Zxing默認的是橫屏掃碼,在實際生產中並不適用,需要改為豎屏掃描。

轉豎屏步驟:

  1>. AndroidManifest.xml中把activity標簽CaptureActivity部分的screenOrientation改為portrait。

android:screenOrientation="portrait"

  2>. CameraManager類中的getFramingRectInPreview()方法,將left, right, top, bottom改變。

1 //豎屏
2 rect.left = rect.left * cameraResolution.y / screenResolution.x;
3 rect.right = rect.right * cameraResolution.y / screenResolution.x;
4 rect.top = rect.top * cameraResolution.x / screenResolution.y;
5 rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;

  3>. CameraConfigurationManager類中的setDesiredCameraParameters(OpenCamera camera, boolean safeMode)方法,在setParameters之前添加

theCamera.setDisplayOrientation(90);

  4>. DecodeHandler類中的decode(byte[] data, int width, int height)方法,在PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height)之前添加

1 byte[] rotatedData = new byte[data.length];
2 for (int y = 0; y < height; y++) {
3 for (int x = 0; x < width; x++)
4    rotatedData[x * height + height - y - 1] = data[x + y * width];
5 }
6 int tmp = width; // Here we are swapping, that's the difference to #11
7 width = height;
8 height = tmp;
9 data = rotatedData;

  此時,豎屏掃描已經可以實現了,但是掃描復雜的圖碼時,分辨率低的已經分不清紋理了,很難識別出來,所以需要優化識別率。

識別率優化:

  1>. CameraConfigurationUtils類中的findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution)方法,將double screenAspectRatio = screenResolution.x / (double) screenResolution.y改為

1 double screenAspectRatio;
2 if (screenResolution.x > screenResolution.y) {
3    screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
4 } else {
5    screenAspectRatio = (double) screenResolution.y / (double) screenResolution.x;
6 }

  2>. 至此,識別率已經很大程度上的提高了,若在要提高識別率,可通過修改CameraManager類中的MAX_FRAME_WIDTH和MAX_FRAME_HEIGHT來提高精度。

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: