当我使用相机意图时,我的图像会旋转。捕获图片后,我通过使用Intent将此图片发送到另一个活动,并将此图片设置为布局的背景,但在横向捕捉图片旋转图片。
答案 0 :(得分:0)
您可以使用下面显示的ExitInterface
解决此问题:
private void setPic(Uri contentUri) throws IOException {
// Get the dimensions of the View
int targetW = uploadedImage.getWidth();
int targetH = uploadedImage.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = calculateInSampleSize(bmOptions, targetW, targetH);
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
bitmap = rotateImageIfRequired(bitmap, contentUri);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, 750, 750);
uploadedImage.setImageBitmap(bitmap);
}
此功能是根据手机中的路径解码位图的位置,然后调用rotateImageIfRequired()
,这将决定图像在设置为布局背景之前应该旋转的方向。在这种情况下,uploadedImage
是您的布局。
private Bitmap rotateImageIfRequired(Bitmap img, Uri contentUri) throws IOException {
ExifInterface ei = new ExifInterface(contentUri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
此功能将确定图像在解码后以及图像设置之前的方向。
private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}
此功能旋转图像并返回新图像。
希望这有帮助。