android zxing 横屏改为竖屏的配置

来源:互联网 发布:复杂网络研究什么 编辑:程序博客网 时间:2024/06/05 03:56

android的zxing扫描demo默认是横屏的,但是竖屏更加符合目前的使用情况,网上也有很多关于改为横屏的文章,但是有些地方的配置是错误的,在此本人在实践的基础上重新整理一份修改说明,以作备忘。

【一】修改 AndroidManifest.xml,改为竖屏

           将扫描的Activity的 

     android:screenOrientation="landscape"

     更改为:

     android:screenOrientation="portrait"

【二】修改 CameraConfigurationManager 类

           1.竖屏后预览方向变为横向的问题

               修改方法 setDesiredCameraParameters,在方法末尾添加以下代码:

       if (Integer.parseInt(Build.VERSION.SDK) >= 8)setDisplayOrientation(camera, 90);else {if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {parameters.set("orientation", "portrait");parameters.set("rotation", 90);}if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {parameters.set("orientation", "landscape");parameters.set("rotation", 90);}}
           2.预览图像拉伸的问题

              修改方法 initFromCameraParameters,在 Log.d(TAG,"Screen resolution: " +screenResolution);后添加以下代码:

        // 竖屏后,预览拉伸的问题---start----        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;        }        // 竖屏后,预览拉伸的问题---end----
               同时修改 cameraResolution = getCameraResolution(parameters, screenResolution);

      为cameraResolution = getCameraResolution(parameters,screenResolutionForCamera);


【三】修改 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

【四】修改 DecodeHandler 类

           修改方法 decode,在 source = CameraManager.get().buildLuminanceSource(data, width, height);

    之前添加以下代码:

     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;     data  = rotatedData;
另外,网上有文章说的修改PlanarYUVLuminanceSource.java中的renderCroppedGreyscaleBitmap方法,将其中的dataWidth和dataHeight互换等的修改都不需要,否则虽然可以正常扫描,但是会影响扫描后图片的生产。

关于更过自定义的修改,可以参考网上其他文章,也感谢网友的分享。

取景框的绘制、连续扫描等可参到以下文章:

http://www.cnblogs.com/dolphin0520/p/3355728.html

http://www.wefashional.com/f/bokeDetail.jhtml?id=257

http://www.2cto.com/kf/201506/404765.html

0 0
原创粉丝点击