1、修改manifest文件,将CaptureActivity设为portrait
[java] view plaincopy
android:screenOrientation=“portrait”
\
2、在DecodeHandler.java文件中,找到decode(byte[],int,int)方法,在buildLuminanceSource调用前,加上如下:
[java] view plaincopy
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()方法,
替换相应代码:
\
[java] view plaincopy
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()方法,加入代码
\
[java] view plaincopy
camera.setDisplayOrientation(90);
到这里在ZXing1.6上已经可以完美的实现了,但是你用的是ZXing2.1的源码代码的话(我说的是ZXing2.1的例子项目),你会发现取景框会在屏幕的右上角,所以我们得把其拿到中间来。且看
\
\ 5、在CameraConfigurationManager.java里找到initFromCameraParameters(Camera camera)方法,去掉如下代码
[java] view plaincopy
if (width < height) {
Log.i(TAG, “Display reports portrait orientation; assuming this is incorrect”);
int temp = width;
width = height;
height = temp;
}
到此就可以了,这样的话,可能你显示出来的大小不是你满意的,你可以修改CameraManager类中的如下代码:
[java] view plaincopy
private static final int MIN_FRAME_WIDTH = 240;
private static final int MIN_FRAME_HEIGHT = 240;
private static final int MAX_FRAME_WIDTH = 400;
private static final int MAX_FRAME_HEIGHT = 600;
你还可以根据像素进行数组选择最合适的宽高值,
\
**\ **
对了,在打Log中遇到了了一个问题,在此提醒下大家
Unexpected error initializing cameraLog tag “CameraConfigurationManager” exceeds limit of 23 characters**\ **LOG竟然还有23个字符的限制、报RuntimeException
http://blog.csdn.net/js931178805/article/details/8215991
PS:竖屏后扫码识别率没这么高了。