最近在開發一個二維碼掃描的項目,Down了一份Zxing的源碼然後進行改寫,在修改Zxing for android豎屏模式,遇到如下問題:
在修改了AndroidMainfest.xml中的Activity的屬性android:screenOrientation="portrait"後,Activity被強制設定為豎屏模式,但是當啟動攝像機的時候,顯示的畫面仍然為橫屏模式。故在網上找了如下方法:
針對ZXing 1.6版本:
1、修改manifest檔案,將CaptureActivity設為portrait
android:screenOrientation="portrait"
2、在DecodeHandler.java檔案中,找到decode(byte[],int,int)方法,在buildLuminanceSource調用前,加上如下:
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 #11width = height;height = tmp;data = rotatedData;
3、在CameraManager.java中找到getFramingRectInPreview()方法, 替換相應代碼:
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;
4、在CameraConfigurationManager.java裡找到setDesiredCameraParameters()方法,加入代碼:(這裡不同的SDK版本可能調用的方法不一樣)
camera.setDisplayOrientation(90);