ZXing竖屏扫描

发布时间:2020-07-06 12:55:38 作者:Soldat_D
来源:网络 阅读:795

zxing实现竖屏扫描

第一步:修改AndroidManifest清单文件,删除AndroidManifest中CaptureActivity的screenOrientation属性:

<activity android:name=".CaptureActivity"  

              android:screenOrientation="landscape"
              android:clearTaskOnLaunch="true"  

              android:stateNotNeeded="true"  

              android:theme="@style/CaptureTheme"  

              android:windowSoftInputMode="stateAlwaysHidden">
<activity android:name=".CaptureActivity"
              android:screenOrientation="portrait"
              android:clearTaskOnLaunch="true"
              android:stateNotNeeded="true"
              android:theme="@style/CaptureTheme"
              android:windowSoftInputMode="stateAlwaysHidden">

 

 

第二步:删除CaptureActivity中把onResume方法中的无用代码:

 

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


第三步:在CaptureActivity中把onCreate方法结尾处添加代码:

 

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {  
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
    } else {  
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
    }
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }


第四步:在CameraConfigurationManager的setDesiredCameraParameters方法中添加代码:

camera.setDisplayOrientation(90);//add 这句代码作用是旋转镜头90度,使相机预览方向正确显示  
camera.setParameters(parameters);
camera.setDisplayOrientation(90);//add 这句代码作用是旋转镜头90度,使相机预览方向正确显示
camera.setParameters(parameters);


第五步:修改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;
//      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(byte[] data, int width, int height)方法:


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


 

推荐阅读:
  1. 利用Zxing扫描和产生二维码
  2. android zxing 4.7.1横屏改竖屏 screenOrientation landscape to portrait

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 4.0+

上一篇:nil和null的区别

下一篇:性能测试-服务器指标

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》