溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ZXing豎屏掃描

發(fā)布時間:2020-07-06 12:55:38 來源:網(wǎng)絡(luò) 閱讀:789 作者:Soldat_D 欄目:移動開發(fā)

zxing實現(xiàn)豎屏掃描

第一步:修改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方法結(jié)尾處添加代碼:

 

    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 這句代碼作用是旋轉(zhuǎn)鏡頭90度,使相機(jī)預(yù)覽方向正確顯示  
camera.setParameters(parameters);
camera.setDisplayOrientation(90);//add 這句代碼作用是旋轉(zhuǎn)鏡頭90度,使相機(jī)預(yù)覽方向正確顯示
camera.setParameters(parameters);


第五步:修改CameraManager中g(shù)etFramingRectInPreview方法:

//      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;


 

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI