我正在开发一款应用程序,用户可以使用手机相机拍照,并在ImageView中显示图片。现在问题是,如果我在纵向或横向模式下捕获图像,它始终会将图像设置为横向模式,但我希望图像仅以纵向模式设置。请帮我解决这个问题。感谢任何帮助... 谢谢。
Matrix mat = new Matrix();
ExifInterface exif = new ExifInterface(yourimagepath);
String orientstring = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientstring != null ? Integer.parseInt(orientstring) : ExifInterface.ORIENTATION_NORMAL;
int rotateangle = 0;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90)
rotateangle = 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_180)
rotateangle = 180;
if(orientation == ExifInterface.ORIENTATION_ROTATE_270)
rotateangle = 270;
mat.setRotate(rotateangle, (float) bmpPic.getWidth() / 2, (float) bmpPic.getHeight() / 2);
File f = new File(yourimagepath);
Bitmap bmpPic = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
Bitmap bmpPic1 = Bitmap.createBitmap(bmpPic, 0, 0, bmpPic.getWidth(), bmpPic.getHeight(), mat, true);
使用方法
Matrix matrix=new Matrix();
imageView.setScaleType(ScaleType.MATRIX); //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);
例如:
matrix.postRotate( 90f, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2)
我找到了一个很好的解决方案:
一行解决方案:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
或者
Picasso.with(context).load("file:" + photoPath).into(imageView);
这将自动检测旋转并将图像放置在正确的方向
Picasso是一个非常强大的库,用于处理应用程序中的图像,包括:最小内存使用的复杂图像转换。它可能需要一秒钟来加载,但我只是在图像视图后面放了一些文本,显示“正在加载图像”,当图像加载时,它会覆盖文本。