我正在使用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. 相機意圖拍攝兩張圖片
- 2. UIImageView從相機拍攝後旋轉
- 3. Android中的攝像頭拍攝的旋轉圖片
- 4. Android旋轉相機拍攝的照片本身
- 5. Android將照片旋轉90度(由相機拍攝)
- 6. 無法拍攝圖片相機
- 7. Android:拍攝風景圖片後發生相機崩潰
- 8. 當我從圖庫中選擇照片或從相機拍攝照片時,我的照片正在旋轉?
- 9. android-ANR從畫廊/相機拍攝圖片
- 10. 延遲和缺失從相機拍攝照片後出現圖片問題
- 11. 圖片旋轉90度拍攝時自拍
- 12. Android自定義相機api以自定義png作爲背景拍攝圖片
- 13. android - 從相機拍攝圖像
- 14. 用Android的相機拍攝灰度圖片2
- 15. 從相機拍攝的iphone圖像自動旋轉-90度
- 16. 在表格視圖中顯示從相機拍攝的照片
- 17. 來自相機拍攝圖像的圖像旋轉問題
- 18. Phonegap相機API保存拍攝到相機膠捲的照片
- 19. 如何從Android上的Instagram API獲取圖片拍攝日期?
- 20. 拍照後更新圖片庫不顯示所有圖片Android相機
- 21. 使用Android相機拍攝照片後開始新活動
- 22. 拍攝一張照片後,Android相機凍結
- 23. 拍攝肖像照片時圖像旋轉90度
- 24. 從網絡攝像機拍攝圖片並使用FTP上傳
- 25. Android MediaStore.ACTION_IMAGE_CAPTURE拍攝的圖片 - 指定圖片大小
- 26. Android:相機打開但不保存拍攝的照片,爲什麼?
- 27. IOS Swift 3:在拍攝相機圖片後水平翻轉前置相機圖像
- 28. 拍攝來自相機的照片
- 29. 相機照片拍攝失敗
- 30. 從角度拍攝文字圖片
圖片旋轉的原因是因爲照相機的方向。你可以讀取圖像exif數據來找出它的方向。 –