前提・実現したいこと
Androidにてアプリ内で撮影した写真をimageViewに表示するシステムを制作しています。
setImageURIを用いて写真を表示すると、テスト端末では画像が90度回転して表示されます。
そのため、BitmapにしてExif情報を読み取ることで対応しましたが、今度は画質(画像サイズ)が著しく低下してしまいました。
BitmapにすることなくExif情報を参照する方法、または画質(画像サイズ)を保ったまま、表示する上のみで90度回転し表示することは可能でしょうか。
imageView自体を回転させたくないため、setRotationは利用したくないと考えています。
下記プログラムでは、変換される前のファイルの解像度は34924656、変換後の解像度は317423になります。
当方初心者につき、必要な情報が不足しておりましたら大変申し訳ありません。その際は迅速に追記いたしますので、何卒お力添えをいただきたいと存じております。よろしくお願いいたします。
カメラ周辺
Java
1private void cameraIntent(){ 2 3 // 保存先のフォルダーを作成 4 File cameraFolder = new File( 5 Environment.getExternalStoragePublicDirectory( 6 Environment.DIRECTORY_PICTURES),"IMG"); 7 cameraFolder.mkdirs(); 8 9 // 保存ファイル名 10 String fileName = new SimpleDateFormat( 11 "ddHHmmss", Locale.US).format(new Date()); 12 filePath = String.format("%s/%s.jpg", cameraFolder.getPath(),fileName); 13 14 // capture画像のファイルパス 15 File cameraFile = new File(filePath); 16 cameraUri = FileProvider.getUriForFile( 17 MainActivity.this, 18 getApplicationContext().getPackageName() + ".fileprovider", 19 cameraFile); 20 21 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 22 intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri); 23 startActivityForResult(intent, RESULT_CAMERA); 24
onActivityResult
Java
1@Override 2 protected void onActivityResult(int requestCode, 3 int resultCode, Intent intent) { 4 if (requestCode == RESULT_CAMERA) { 5 6 if(cameraUri != null){ 7 8 rotateImage(filePath); 9 imageView.setImageURI(cameraUri); 10 registerDatabase(filePath); 11 12 } 13 else{ 14 Log.d("debug","cameraUri == null"); 15 } 16 } 17 }
画像回転
Java
1public int rotateImage(String filePath){ 2 3 int degree = getRotateDegree(filePath); 4 Log.d("debug","degree:"+degree); 5 6 if (degree > 0){ 7 OutputStream out = null; 8 Bitmap bitmap = null; 9 Bitmap rotatedImage = null; 10 try{ 11 Matrix mat = new Matrix(); 12 mat.postRotate(degree); 13 BitmapFactory.Options opts = new BitmapFactory.Options(); 14 opts.inJustDecodeBounds = true; 15 BitmapFactory.decodeFile(filePath, opts); 16 int width = 480; 17 int scale = 1; 18 if (opts.outWidth > width) { 19 scale = opts.outWidth / width + 2; 20 } 21 opts.inJustDecodeBounds = false; 22 opts.inSampleSize = scale; 23 bitmap = BitmapFactory.decodeFile(filePath, opts); 24 rotatedImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true); 25 out = new FileOutputStream(filePath); 26 rotatedImage.compress(Bitmap.CompressFormat.JPEG, 100, out); 27 } catch (Exception ex) { 28 ex.printStackTrace(); 29 } finally { 30 if (out != null) try { out.close(); } catch (IOException e) {} 31 if (bitmap != null) bitmap.recycle(); 32 if (rotatedImage != null) rotatedImage.recycle(); 33 } 34 35 } 36 37 return degree; 38 }
Java
1private int getRotateDegree(String filePath){ 2 3 int degree = 0; 4 try{ 5 ExifInterface exifInterface = new ExifInterface(filePath); 6 int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 7 ExifInterface.ORIENTATION_UNDEFINED); 8 9 if (orientation == ExifInterface.ORIENTATION_ROTATE_90){ 10 degree = 90; 11 } 12 else if (orientation == ExifInterface.ORIENTATION_ROTATE_180){ 13 degree =180; 14 } 15 else if (orientation == ExifInterface.ORIENTATION_ROTATE_270){ 16 degree = 270; 17 } 18 if (degree != 0){ 19 exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "0"); 20 exifInterface.saveAttributes(); 21 } 22 } catch (IOException e){ 23 degree = -1; 24 e.printStackTrace(); 25 } 26 27 return degree; 28 }
試したこと
Uri形式の画像ファイルを回転させる方法を調べましたが、どれもBitmap形式にしてExif情報から...というものしか見つけ出せず、かつ画質の劣化しない方法を見つけ出すに至ることができませんでした。
補足情報
・開発環境
Android Studio 3.1.3
Build #AI-173.4819257, built on June 5, 2018
JRE: 1.8.0_152-release-1024-b02 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1
・テスト端末
LG V20(LGE-H990)
Android6.0
・アプリ対応Sdk23↑
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/25 07:52
2018/07/25 16:53
2018/07/27 02:38
2018/07/27 04:47 編集