我正在使用android:hardware:camera API来制作自定义相机。我已经做了相机,但问题是,从相机拍摄照片后,图片保存在旋转的形式。我使用相机的表面视图。 这是相机的设置功能上表面形成为什么图片旋转后从Android拍摄图片:硬件:相机api
2
A
回答
3
try {
File f = new File(imagePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
null, options);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
outstudentstreamOutputStream);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}
试试这个从后摄像机捕获图像旋转。
+0
感谢兄弟的工作 –
+0
okk兄如果我的答案可以接受请投票。 – aj0822ArpitJoshi
2
您可以使用以下方法以实际方向显示/保存图像。
public static Bitmap setImage(String filePath, Bitmap bitmap){
File curFile = new File(filePath);
Bitmap rotatedBitmap = null;
try {
ExifInterface exif = new ExifInterface(curFile.getPath());
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
Matrix matrix = new Matrix();
if (rotation != 0.0f) {matrix.preRotate(rotationInDegrees);}
rotatedBitmap = Bitmap.createBitmap(bitmap,0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}catch(IOException ex){
ex.printStackTrace();
}
return rotatedBitmap;
}
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}
相关问题
- 1. 上传图片拍摄从android手机
- 2. 相机意图拍摄两张图片
- 3. 从相机拍摄照片
- 4. UIImageView从相机拍摄后旋转
- 5. Android中的摄像头拍摄的旋转图片
- 6. Android将照片旋转90度(由相机拍摄)
- 7. Android旋转相机拍摄的照片本身
- 8. 获取相机拍摄的图片
- 9. 无法拍摄图片相机
- 10. 相机拍摄照片在Android临时保存图片?
- 11. Android:拍摄风景图片后发生相机崩溃
- 12. 为什么原生相机在保存拍摄图像后旋转
- 13. 当我从图库中选择照片或从相机拍摄照片时,我的照片正在旋转?
- 14. 使用相机拍摄照片后,意图为空
- 15. android-ANR从画廊/相机拍摄图片
- 16. 的Android相机拍摄多张照片
- 17. 使用android相机拍摄android中的图片
- 18. 延迟和缺失从相机拍摄照片后出现图片问题
- 19. 图片旋转90度拍摄时自拍
- 20. Android自定义相机api以自定义png作为背景拍摄图片
- 21. android - 从相机拍摄图像
- 22. 图片拍摄后相机活动中的对话框
- 23. 用Android的相机拍摄灰度图片2
- 24. 从相机拍摄照片后,它不会在图像视图中设置
- 25. 从相机拍摄的iphone图像自动旋转-90度
- 26. 来自相机拍摄图像的图像旋转问题
- 27. 如何从照相机拍摄的照片制作缩略图?
- 28. 在表格视图中显示从相机拍摄的照片
- 29. 使用相机捕捉图片后旋转图片并保存为PNG格式
- 30. 在android中的相机拍摄的imageview照片中显示图片
图片旋转的原因是因为照相机的方向。你可以读取图像exif数据来找出它的方向。 –