Android setRotation(90) 在人像模式下拍照在三星设备上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11023696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setRotation(90) to take picture in portrait mode does not work on samsung devices
提问by tofi9
According to the documentation, setRotation(90)should rotate the captured JPEG picture (takePicturein landscape mode.
根据文档,setRotation(90)应该旋转捕获的 JPEG 图片(横向模式下的takePicture。
This works fine on a HTC phone, but does not work on Samsung Google Nexus S and Samsung Galaxy S3. Is this a bug?
这适用于 HTC 手机,但不适用于三星 Google Nexus S 和三星 Galaxy S3。这是一个错误吗?
I know that I can use the matrix transform rotation, but wish the OS can do this more efficiently, and don't want to risk over-rotating on some other devices.
我知道我可以使用矩阵变换旋转,但希望操作系统可以更有效地执行此操作,并且不想在其他一些设备上冒过度旋转的风险。
edit
编辑
Setting camera.setDisplayOrientation(90);
made the preview to be in portrait mode, however it did not have any affect on the picture taken.
设置camera.setDisplayOrientation(90);
使预览处于纵向模式,但它对拍摄的照片没有任何影响。
Further, Besides setRotation
, I have also tried to set the picture size - where I flip h
with w
: parameters.setPictureSize(1200, 1600);
. This also did not have any affect.
此外,此外setRotation
,我还尝试设置图片大小 - 我h
用w
:翻转的地方parameters.setPictureSize(1200, 1600);
。这也没有任何影响。
solution
解决方案
Apparently Samsung phones set the EXIF orientation tag, rather than rotating individual pixels. As ariefbayu
suggested, reading the Bitmap using BitmapFactory
does not support this tag. His code sample is the solution, and this solution is also compatible with using inSampleSize
.
显然,三星手机设置了 EXIF 方向标签,而不是旋转单个像素。正如所ariefbayu
建议的,使用读取位图BitmapFactory
不支持此标签。他的代码示例就是解决方案,这个解决方案也兼容使用inSampleSize
.
采纳答案by ariefbayu
I try to answer this in relation to the Exif tag. This is what I did:
我尝试就 Exif 标签回答这个问题。这就是我所做的:
Bitmap realImage = BitmapFactory.decodeStream(stream);
ExifInterface exif=new ExifInterface(getRealPathFromURI(imagePath));
Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
realImage=ImageUtil.rotate(realImage, 90);
}else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
realImage=ImageUtil.rotate(realImage, 270);
}else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
realImage=ImageUtil.rotate(realImage, 180);
}
The ImageUtil.rotate()
:
的ImageUtil.rotate()
:
public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
回答by edid
Liso22, the variable stream doesn't make a difference, just plug in your bitmap, however you got it (decodeFile etc). And if you're having problems with the 'ImageUtil.rotate(), just write the 'public static Bitmap rotate()' as a method with the same parameters and make 'real image' equal that. Anyways, this solution doesn't seem to be working for me, the exif tag returns 1 (normal) whether in portrait or landscape.
Liso22,变量流没有区别,只需插入您的位图,但是您得到了它(decodeFile 等)。如果您在使用“ImageUtil.rotate()”时遇到问题,只需将“public static Bitmap rotate()”编写为具有相同参数的方法,并使“真实图像”与此相同。无论如何,这个解决方案似乎对我不起作用,无论是纵向还是横向,exif 标签都返回 1(正常)。