zxing横竖屏问题

首先我在github上下载zxing的时间是2014年8月,版本好像是zxing-3。

个人参考了这篇文章,很不错

http://blog.csdn.net/chenbin520/article/details/16362459

但是还是有点问题,毕竟年代久远了点,zxing源代码是有一定修改的。

其实我稍微修改的地方就是CaptureActivity的onResume方法,找到

if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
	setRequestedOrientation(getCurrentOrientation());
} else {
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);		
}
直接替换成

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
这样xml里面都不用指定方向了。其他大家参考上面链接的文章。我这里也把我的步骤贴出来。

1.如上,替换CaptureActivity的onResume里面的

if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
	setRequestedOrientation(getCurrentOrientation());
} else {
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);		
}

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
这时屏幕就变成竖屏了,但是相机成像的图像却旋转了。

2.找到这个类CameraConfigurationManager的这个方法setDesiredCameraParameters,我是在camera.setParameters(parameters);后面添加了如下代码camera.setDisplayOrientation(90);其他地方插入会不会有影响没测试,这时候相机拍摄成像方向正常了。改完这两个地方好像可以啦,但是图像有点拉伸,扫二维码好像也没问题,但是扫竖线条码却还是要把手机转动90度,咱们继续。

3.继续在CameraConfigurationManager里面找到initFromCameraParameters方法,把

cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(
				parameters, screenResolution);

替换成

if (screenResolution.x < screenResolution.y) {      
			screenResolutionForCamera.x = screenResolution.y;      
			screenResolutionForCamera.y = screenResolution.x;   
		}    
		cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(
				parameters, screenResolutionForCamera);

这样子屏幕垂直方向的拉伸就没了。

4.然后找到CameraManager这个类,把getFramingRectInPreview里的这段

rect.left = rect.left * cameraResolution.x / screenResolution.x;
      rect.right = rect.right * cameraResolution.x / screenResolution.x;
      rect.top = rect.top * cameraResolution.y / screenResolution.y;
      rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;

替换为

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;

5.最后找到DecodeHandler这个类,把decode里的

PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);

这段替换为

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];
    }
    
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, height, width);


经过这几步,我手机上是没问题了,小伙伴,你的问题解决没?








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值