Nexus 手機 原生Android系統 Android 8.0android
測試發如今Nexus手機,android8.0手機上發現,掃碼的時候顯示的拍照預覽方向不正,有180度的旋轉而且變形。 可是在其餘手機及系統上測試是沒有問題的。安全
仍是依舊Stack Overflow啊。 親測完美解決此問題。bash
private void surfaceIsChanged() {
if (mHolder.getSurface() == null) {
System.out.println("getSurface,nullnull");
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
}
try {
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
int dataBufferSize = (int) (previewSize.height * previewSize.width * (ImageFormat.getBitsPerPixel(mCamera.getParameters()
.getPreviewFormat()) / 8.0));
mCamera.addCallbackBuffer(new byte[dataBufferSize]);
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallbackWithBuffer(previewCallback);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int camIdx = 0; // DO your logic to get front or back camera...or loop through all avaialable.
Camera.getCameraInfo(camIdx, cameraInfo);
try {
// If using back camera then simply rotate what CameraInfo tells you.
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
mCamera.setDisplayOrientation(cameraInfo.orientation);
else
// If using front camera note that image might be flipped to give users the impresion the are looking at a mirror.
mCamera.setDisplayOrientation( (360 - cameraInfo.orientation) % 360);
} catch (Exception e) {
e.printStackTrace();
}
// Toast.makeText(QRZbarActivity.this, "開始掃描",
// Toast.LENGTH_SHORT).show();
autoOpenLight();
} catch (Exception e) {
Toast.makeText(BaseScanActivity.this, R.string.account_toast_not_open_camera, Toast.LENGTH_SHORT).show();
// showTip("您未容許" + getResources().getString(R.string.app_name) + "訪問您的相冊\n請在「安全中心 -受權管理」中更改設置");
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
複製代碼
其中主要生效的代碼是:app
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int camIdx = 0; // DO your logic to get front or back camera...or loop through all avaialable.
Camera.getCameraInfo(camIdx, cameraInfo);
try {
// If using back camera then simply rotate what CameraInfo tells you.
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
mCamera.setDisplayOrientation(cameraInfo.orientation);
else
// If using front camera note that image might be flipped to give users the impresion the are looking at a mirror.
mCamera.setDisplayOrientation( (360 - cameraInfo.orientation) % 360);
} catch (Exception e) {
e.printStackTrace();
}
複製代碼
根據照相的內容進行設置顯示方向。oop
參考:解決方法測試
// END 2018年4月19日ui