【发布时间】:2017-03-20 12:40:32
【问题描述】:
之前我的代码遇到了一个问题,当我使用前置摄像头拍摄照片时,将图片设置为 ImageView 时,旋转被翻转并且图像使用镜像效果。
经过一些研究后问题得到解决,但是我仍然遇到图像在预览屏幕上翻转的问题。
启动活动以获得结果..(用户看到相机)
用户拍照(显示预览屏幕,这是我看到镜像效果的地方)
用户单击确定.. ImageView 已设置.. 此处图像设置完美,没有镜像效果问题
下面是我的代码..
private void takePhoto(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
uri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath(),
options);
Matrix matrix = new Matrix();
float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
Matrix matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
matrix.postConcat(matrixMirrorY);
ExifInterface exifInterface = null;
try {
exifInterface = new ExifInterface(uri.getPath());
} catch (IOException e) {
e.printStackTrace();
}
assert exifInterface != null;
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(270);
break;
}
rotatedBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
visitorImage.setImageBitmap(rotatedBitmap);
visitorPreviewImage.setImageBitmap(rotatedBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
}
我的问题是我目前不确定在哪里可以解决预览屏幕的镜像效果问题..
谢谢大家。
【问题讨论】: