二維碼橫豎屏掃描

橫屏顯示:

“`
DecodeHandler.java:
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);

CameraManager.java
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;

“`在AndroidManifest.xml中,把Activity的屬性android:screenOrientation=”landscape”

/****************/
豎屏顯示:
1、修改AndroidManifest文件,將CaptureActivity設爲portrait:
代碼爲:android:configChanges=”orientation|keyboardHidden”
android:screenOrientation=”portrait”

2、把攝像頭預覽景調爲豎向,具體如下:
1)在CameraConfigurationManager類裏的setDesiredCameraParameters(Camera camera)方法裏添加如下代 碼:
//攝像頭旋轉90度
setDisplayOrientation(camera, 90);
2)然後在CameraConfigurationManager類裏面添加setDisplayOrientation(Camera camera, int angle)方法,方法具體代碼如下:

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) {
e1.printStackTrace();
}
}
3)最後,爲了防止攝像頭豎過來後圖像拉伸的問題,需要在CameraConfigurationManager類中的initFromCameraParameters(Camera camera)方法的Log.d(TAG, “Screen resolution: ” + screenResolution);語句後面添加如下代碼:
//解決豎屏後圖像拉伸問題
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;
}
cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);
3、把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;
4、在DecodeHandler類中的decode(byte[] data, int width, int height)方法中,在buildLuminanceSource調用前添加如下代碼:
//豎屏
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;

如有不足歡迎指正。。。。

發佈了32 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章