Android Zxing 扫描条码实现竖屏模式 Camera摄像头 旋转90度

该文章转自网络

关于条形码扫描的软件,做软件需求时需要扫描时竖屏:       

android提供的SDK(android.hardware.Camera)里大概不能正常的使用竖屏(portrait layout)加载照相机,当用竖屏模式加载照相机时会产生以下情况:

1. 照相机成像左倾90度(倾斜);

2. 照相机成像长宽比例不对(失比)。

基本上解决办法如下:

1、在AndroidManifest.xml里面配置一下 ,使CaptureActivity属性为portrait:   android:screenOrientation="portrait"

2、如果只是单纯的想改变照相机成像的方向,只需要在包com.google.zxing.client.android.camera下的   CameraConfigurationManager类中增加方法   

protected void setDisplayOrientation(Camera camera, int angle) {

        Method downPolymorphic;        

   try {         

    downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });

           if (downPolymorphic != null)          

    downPolymorphic.invoke(camera, new Object[] { angle });        

  } catch (Exception e1) {         }       

}   

 

然后在方法void setDesiredCameraParameters(Camera camera){}中调用, setDisplayOrientation(camera, 90);    

 具体位置在camera.setParameters(parameters);语句前面。一般到这里,就已经可以解决向左旋转90度的问题了。

3、改变完方向你会发现方向改变了可是分辨率会变得很低,接下来就是优化了  

1)首先在类CameraManager.java中    把

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;   

(2)然后是在DecodeHandler类中的方法private void decode(byte[] data,int width,int 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];   

(3)再就是CameraConfigurationManager类中的方法void initFromCameraParameters(Camera camera){}中添加如下代码:

Point screenResolutionForCamera = new Point();    

screenResolutionForCamera.x = screenResolution.x;    

screenResolutionForCamera.y = screenResolution.y;    

// preview size is always something like 480*320, other 320*480 if (screenResolution.x < screenResolution.y) {   screenResolutionForCamera.x = screenResolution.y;   screenResolutionForCamera.y = screenResolution.x; 注: 到这就完成了。如果还有问题,只需下载资源文件直接替换相应文件即可;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值