關於條碼掃描Zxing,需要掃描時直立顯示:
android提供的SDK(android.hardware.Camera)裡大概不能正常的使用直立顯示(portrait layout)加載照相機,當用直立顯示模式加載照相機時會產生以下情況:
1.照相機成像左傾90度(傾斜);
2.照相機成像長寬比例不對(失比)。
基本上解決辦法如下:
1、在AndroidManifest.xml裡面配置一下 ,使CaptureActivity属性為portrait:android:screenOrientation="portrait"
2、如果只是單純的想改變照相機成像的方向,只需要在com.google.zxing.client.android.camera下的 CameraConfigurationManager類別中增加方法
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) { }
}
然後在方法void setDesiredCameraParameters(Camera camera){}中調用, setDisplayOrientation(camera, 90); 具體位置在camera.setParameters(parameters);語句前面。
3、改變完方向你會發現方向改變可可分辨率會變得很低,接下來就是優化了
1)首先在類別CameraManager.java中把
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;
(2)然後是在DecodeHandler類別中的方法private void decode(byte[] data,int width,int 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];
(3)再就是CameraConfigurationManager類別中的方法void initFromCameraParameters(Camera camera){}中添加如下代碼:
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;
沒有留言:
張貼留言