ZXING3.22 横屏改竖屏扫描

因版本差异,扫描效果略有不同,我测试的时候使用的是ZXING3.22,与原文作者使用版本一致,修改后,测试通过,在此仅作记录,并感谢原作者的无私贡献,谢谢!

转自:https://blog.csdn.net/xiaojun8547093/article/details/48784431?ticket=ST-75335-QFEIquoDoNoA5L3R6cqM-passport.csdn.net

原文:
在网上找了很多源码,也找了很多很多修改的方法,但都会出现复杂的二维码扫描不出来,或者扫描很慢的问题,所以,自己把现在修改的整理下,以后自己好查看. 

1.先修改CameraConfigurationManager类 

cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
//修改为
    Point screenResolutionForCamera = new Point();
    screenResolutionForCamera.x = screenResolution.x;
    screenResolutionForCamera.y = screenResolution.y;
    if (screenResolution.x < screenResolution.y) {
      screenResolutionForCamera.x = screenResolution.y;
      screenResolutionForCamera.y = screenResolution.x;
    }
    cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolutionForCamera);
        /*结束*/


2.在setDesiredCameraParameters方法中添加 
在第一行添加就可以了

    camera.setDisplayOrientation(90);
      /*结束*/

3. 修改CameraManager类

//      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.修改DecodeHandler类

//    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];
    }
    int tmp = width;
    width = height;
    height = tmp;

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

5.修改CaptureActivity类

//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
//        return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        return  ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
//        return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;        
        return  ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
6.最后将清单文件AndroidManifest.xml

> android:screenOrientation="portrait"`
运行,测试成功!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值