1、修改AndroidManifest.xml文件,将CaptureActivity的android:screenOrientation属性改为portrait
2、在DecodeHandler.java文件中,找到decode(byte[],int,int)方法,在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;
- data = rotatedData;
3、在CameraManager.java中找到getFramingRectInPreview()方法, 替换相应代码:
- 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、在CameraConfigurationManager.java里找到setDesiredCameraParameters()方法,在camera.setParameters(parameters)之前加入:
- camera.setDisplayOrientation(90);
5、在CameraConfigurationManager.java里找到initFromCameraParameters(Camera camera)方法,改为:
- Camera.Parameters parameters = camera.getParameters();
- WindowManager manager = (WindowManager) context
- .getSystemService(Context.WINDOW_SERVICE);
- Display display = manager.getDefaultDisplay();
- int width = display.getWidth();
- int height = display.getHeight();
- // We're landscape-only, and have apparently seen issues with display
- // thinking it's portrait
- // when waking from sleep. If it's not landscape, assume it's mistaken
- // and reverse them:
- if (width < height) {
- Log.i(TAG,
- "Display reports portrait orientation; assuming this is incorrect");
- int temp = width;
- width = height;
- height = temp;
- }
- screenResolution = new Point(height, width);
- Log.i(TAG, "Screen resolution: " + screenResolution);
- cameraResolution = findBestPreviewSizeValue(parameters, new Point(width, height));
- Log.i(TAG, "Camera resolution: " + cameraResolution);