最近的一個Android需要用到掃碼功能,用的是Zxing開源庫。Zxing的集成就不說了,但是Zxing默認的是橫屏掃碼,在實際生產中并不適用,需要改為豎屏掃描。
轉豎屏步驟:
1>. AndroidManifest.xml中把activity標簽CaptureActivity部分的screenOrientation改為portrait。
1
|
android:screenOrientation="portrait" |
2>. CameraManager類中的getFramingRectInPreview()方法,將left, right, top, bottom改變。
1
2
3
4
5
|
//豎屏 rect.left = rect.left * cameraResolution.y / screenResolution.x; rect.right = rect.right * cameraResolution.y / screenResolution.x; rect.top = rect.top * cameraResolution.x / screenResolution.y; rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y; |
3>. CameraConfigurationManager類中的setDesiredCameraParameters(OpenCamera camera, boolean safeMode)方法,在setParameters之前添加
1
|
theCamera.setDisplayOrientation( 90 ); |
4>. DecodeHandler類中的decode(byte[] data, int width, int height)方法,在PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height)之前添加
1
2
3
4
5
6
7
8
9
|
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; // Here we are swapping, that's the difference to #11 width = height; height = tmp; data = rotatedData; |
此時,豎屏掃描已經可以實現了,但是掃描復雜的圖碼時,分辨率低的已經分不清紋理了,很難識別出來,所以需要優化識別率。
識別率優化:
1>. CameraConfigurationUtils類中的findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution)方法,將double screenAspectRatio = screenResolution.x / (double) screenResolution.y改為
1
2
3
4
5
6
|
double screenAspectRatio; if (screenResolution.x > screenResolution.y) { screenAspectRatio = ( double ) screenResolution.x / ( double ) screenResolution.y; } else { screenAspectRatio = ( double ) screenResolution.y / ( double ) screenResolution.x; } |
2>. 至此,識別率已經很大程度上的提高了,若在要提高識別率,可通過修改CameraManager類中的MAX_FRAME_WIDTH和MAX_FRAME_HEIGHT來提高精度。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/fx-blog/p/9037937.html