Android开源二维码识别项目zxing横屏改为竖屏识别解决方案

转载:http://www.cnblogs.com/moka/archive/2013/05/24/3096937.html


首先你需要有zxing项目的简化版代码,在这里

使用简化版可以免去许多不必要的代码,方便学习研究,更好定位核心功能。

如果你调试成功后,就可以着手修改将其变为竖屏识别了。

第1步:

在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改:

android:screenOrientation="portrait" 

 

第2步:

我们要把摄像头预览景调为竖向

CameraConfigurationManager类中的setDesiredCameraParameters()方法中添加如下代码:

// 使摄像头旋转90度
    setDisplayOrientation(camera, 90);

然后在CameraConfigurationManager类的最后添加setDisplayOrientation()方法:

  View Code

 

最后在CameraConfigurationManager中的initFromCameraParameters()方法的Log.d(TAG, "Screen resolution: " + screenResolution);句后面添加如下代码,这段代码是为了解决摄像头竖过来后图像拉伸的问题:

  View Code

 

第3步:

CameranManager类中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;   

 

第4步:

PlanarYUVLuminanceSource类中的getRow()方法为识别条形码部分,

getMatrix()方法为识别二维码部分

renderCroppedGreyscaleBitmap()方法为生成获取的码图部分

将getRow()中的:

int offset = (y + top) * dataWidth + left;

getMatrix()中的:

int inputOffset = top * dataWidth + left;
inputOffset += dataWidth;

renderCroppedGreyscaleBitmap()中的:

int inputOffset = top * dataWidth + left;
inputOffset += dataWidth;

这些语句中dataWidth全部替换为dataHeight

同时将PlanarYUVLuminanceSource构造方法中:

if (left + width > dataWidth || top + height > dataHeight) {
      throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
    }

dataWidth与dateHeight中互换位置即可。

此时,你的程序竖屏识别码图应该没有任何问题了。至于取景框的样式,大家可以在自定义的ViewfinderView中修改成自己喜欢的样式。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值