修改ZXing for Android为竖屏模式

最近在开发一个二维码扫描的项目,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 #11
width = 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);

你可能感兴趣的:(修改ZXing for Android为竖屏模式)