如何将拍摄的图片设置为竖屏模式

3
我正在开发一款应用程序,用户可以使用手机相机拍照,并在ImageView中显示图片。现在问题是,如果我在纵向或横向模式下捕获图像,它始终会将图像设置为横向模式,但我希望图像仅以纵向模式设置。请帮我解决这个问题。感谢任何帮助... 谢谢。
3个回答

5
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);   

谢谢你的回答,但是如何将Mat值添加到位图中?能否请您发布一段代码片段? - Salman Khan
你能否发布一下?我做不了这个。 - Salman Khan

2

使用方法

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)

这里应该是什么角度,pivX和pivY? - Salman Khan
抱歉,我没有看到例子。 - Salman Khan
鼠标悬停在Eclipse中的postRotate()上,就可以轻松查看详细信息了。 - Sunil Kumar

-1

我找到了一个很好的解决方案:

https://dev59.com/3WYq5IYBdhLWcg3w8VG8#34241250

一行解决方案:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

或者

Picasso.with(context).load("file:" + photoPath).into(imageView);

这将自动检测旋转并将图像放置在正确的方向

Picasso是一个非常强大的库,用于处理应用程序中的图像,包括:最小内存使用的复杂图像转换。它可能需要一秒钟来加载,但我只是在图像视图后面放了一些文本,显示“正在加载图像”,当图像加载时,它会覆盖文本。


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅链接的答案可能会失效。- 来自审查 - Suraj Rao
@suraj 感谢您的建议,我已经更新了我的帖子。 - Andrew Moreau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接