Nexus Android8.0システムの写真プレビュー方向の問題解決方法
3318 ワード
Nexus
Nexus携帯原生AndroidシステムAndroid 8.0
に質問
Nexus携帯原生AndroidシステムAndroid 8.0
に質問
テストではNexus携帯電話、android 8.0携帯電話では、スキャン時に表示される写真のプレビュー方向が正しくなく、180度回転して変形していることが分かった.しかし、他の携帯電話やシステムでテストするのは問題ありません.
解決する
相変わらずStack Overflowですね.親測はこの問題を完璧に解決した.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) + "
“ - ” ");
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
主に有効なコードは次のとおりです.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();
}
写真の内容に応じて表示方向を設定します.
参考:解決方法
//END 2018年4月19日
相変わらずStack Overflowですね.親測はこの問題を完璧に解決した.
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) + "
“ - ” ");
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
主に有効なコードは次のとおりです.
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();
}
写真の内容に応じて表示方向を設定します.
参考:解決方法
//END 2018年4月19日