【发布时间】:2019-08-14 09:23:39
【问题描述】:
我需要一种方法来在我捕获图片后自动旋转它。
它需要自动拾取旋转并纠正它。
当我拍照时,它会逆时针旋转 90 度。
private void CompressAndSetImage(Uri uri)
{
Bitmap thumbnail = null;
try {
thumbnail = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), uri);
int nh = (int) ( thumbnail.getHeight() * (1024.0 / thumbnail.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(thumbnail, 1024, nh, true);
Bitmap squareImage = cropToSquare(scaled);
CurrImageURI = getImageUri(getActivity().getApplicationContext(), squareImage);
iv_child.setImageURI(CurrImageURI);
isImageUploaded = true;
} catch (IOException e) {
Toast.makeText(getContext(), "Some error occured", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
private Bitmap cropToSquare(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = (height > width) ? width : height;
int newHeight = (height > width)? height - ( height - width) : height;
int cropW = (width - height) / 2;
cropW = (cropW < 0)? 0: cropW;
int cropH = (height - width) / 2;
cropH = (cropH < 0)? 0: cropH;
Bitmap cropImg = Bitmap.createBitmap(bitmap, cropW, cropH, newWidth, newHeight);
return cropImg;
}
private Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
谢谢。
【问题讨论】:
-
你的意思是,当从相机捕捉图像时,旋转错误。所以你想旋转它的正确位置?
-
当我从相机拍摄时,它会显示正确的视图,但是当它进入我的图像视图时它会旋转
标签: java android android-studio