Java 相机拍照后如何使用BitmapFactory停止图像旋转?
当我用相机在像素3XL上拍摄照片时,在下一页显示时,它会旋转90度(在本例中为Java 相机拍照后如何使用BitmapFactory停止图像旋转?,java,android,Java,Android,当我用相机在像素3XL上拍摄照片时,在下一页显示时,它会旋转90度(在本例中为EditorActivity.class) 我试图通过简单地添加flipIMage方法来解决这个问题,但它似乎没有任何作用 if(resultCode == RESULT_OK){ bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+ &qu
EditorActivity.class
)
我试图通过简单地添加flipIMage方法来解决这个问题,但它似乎没有任何作用
if(resultCode == RESULT_OK){
bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+ "/photoTemp.png");
flipIMage(bitmap);
Intent intent = new Intent(this, EditorActivity.class);
startActivity(intent);
}
flipIMage是
public static Bitmap flipIMage(Bitmap bitmap) {
Matrix matrix = new Matrix();
int rotation = fixOrientation(bitmap);
matrix.postRotate(rotation);
matrix.preScale(-1, 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
如果有帮助,我的cameraClicked方法就在这里
public void cameraClicked(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempFile = new File(Environment.getExternalStorageDirectory().getPath()+ "/photoTemp.png");
try {
tempFile.createNewFile();
Uri uri = FileProvider.getUriForFile(
this,
this.getApplicationContext()
.getPackageName() + ".provider", tempFile);
//install.setDataAndType(uri, mimeType);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, 2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fixOrientation
方法
private static int fixOrientation(Bitmap bitmap) {
if (bitmap.getWidth() > bitmap.getHeight()) {
return 90;
}
return 0;
}
编辑活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
setContentView(R.layout.activity_editor);
Bitmap bitmap = MainActivity.bitmap;
//
btnDoneOrEdit = (Button) findViewById(R.id.btnDoneOrEdit);
btnChangeMask = (Button) findViewById(R.id.btnChangeMask);
editorImage = (EditorImage)findViewById(R.id.editorImage);
editorImage.setBitmapImage(bitmap);
editorImage.setTopPanel((LinearLayout) findViewById(R.id.topPanel));
editorImage.setBottomPanel((LinearLayout) findViewById(R.id.bottomPanel));
gridview_zombie = (GridView)findViewById(R.id.gridView_zombie);
linearcontent = (LinearLayout)findViewById(R.id.linearlayout_content);
在
编辑器.class
上重新加载图像时,可以尝试检查图像上的EXIF数据并在显示之前旋转图像
要从位图获取路径,请使用:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public static Bitmap rotateImage(Bitmap bitmap, String path) throws IOException {
int rotate = 0;
ExifInterface exif;
exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
然后可以使用返回的位图并显示该位图
因此,您的调用堆栈将是
Uri uri = getimageUri(this, MainActivity.Bitmap);
String path = getRealPathFromUri(uri);
Bitmap rotatedBitmap = rotateImage(MainActivity.Bitmap, path);
editorImage.setBitmapImage(bitmap);
试试这个
//check the rotation of the image and display it properly
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
true);
} catch (IOException e) {
e.printStackTrace();
}
使用缩放位图进行进一步处理。fixOrientation()方法在哪里?现在添加了fixOrientation()方法
编辑器活动的onCreate
方法显示Bitmap Bitmap=MainActivity.Bitmap
如果我使用上面的方法检查EXIF,它会工作,除非它正在查找(路径)
,它不会让我将位图放入(路径)
?你能发布编辑器活动
?查看我的更新答案吗?我无法在注释中使用这两种方法。谢谢你,我唯一的问题是这一行exif=newexifinterface(路径)
我不知道在(路径)
中放置什么?如果我不清楚您是否会调用getImageUri(这个,位图)
其中bitmap
是MainActivity.bitmap
并使用返回的URI并将其传递给getRealPathFromURI(URI)
返回的字符串值是您传递的路径rotateimage()
我将重新格式化答案,以便更容易理解