2017-02-15 4 views
1

私は写真を撮り、私が望むように垂直型のサンプルを撮って、水平ラップを行い、練習練習を望むのと同じように垂直写真を保存します、アクティビティはViewerActivityですここで問題がImageViewで表示される瞬間から水平サンプル:/彼が読んでいて、画像の向きを制御できるExifInterfaceクラスがありますが、Imageがまだ水平であるonActivityResult with ImageView

このクラスは写真を担当しています。

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { 
     try { 
      ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath()); 
      int valor = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
      switch (valor) { 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        orientation = 90; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        orientation = 180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        orientation = 270; 
        break; 
      } 
     } catch (Exception e) { 
      Log.d("mensaje", e.toString()); 
     } 
     Intent i = new Intent(this, ViewerActivity.class); 
     i.putExtra(EXTRA_PHOTO_PATH, photoFile.getAbsolutePath()); 
     i.putExtra(EXTRA_PHOTO_ORIENTATION, orientation); 
     i.putExtra(EXTRA_PHOTO_EDIT, false); 
     startActivityForResult(i, 10); 

このViewerActivityクラスには、写真を表示し、それ

private void sendPicture() { 
    Intent intent = new Intent(); 
    intent.putExtra("path", localPath); 
    intent.putExtra("orientation",orientation); 
    setResult(Activity.RESULT_OK, intent); 
    finish(); 
} 

答えて

0

はこれを試してみてください送信しますfileはイメージファイルの名前です

BitmapFactory.Options bounds = new BitmapFactory.Options(); 
bounds.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(file, bounds); 

BitmapFactory.Options opts = new BitmapFactory.Options(); 
Bitmap bm = BitmapFactory.decodeFile(file, opts); 
ExifInterface exif = new ExifInterface(file); 
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL; 

int rotationAngle = 0; 
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90; 
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180; 
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270; 

Matrix matrix = new Matrix(); 
matrix.setRotate(rotationAngle, (float) bm.getWidth()/2, (float) bm.getHeight()/2); 
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);` 

+0

それを試してみて、この方法については私の他の疑いが生じ、中に実装することができ、以下のようなRotateLayoutオブジェクトに、Javaコードから動的に角度値を設定することができます画像をキャプチャするとき、またはすでにimageViewに送信したときの部分ですか?あなたのサポートに感謝します – Devix

+0

結果のビットマップをファイルに変換してください。 –

0
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"); 
} 

これを試してみてください、あなたがonActivityResult

+0

こんにちは、私は方法を実装している瞬間私はそれがどのように出てくると私はあなたに感謝を言うでしょう – Devix

1

使用RotateLayoutでのImagePathを取得する際に、完全なクラスおよびサンプルは下記のリンクで入手可能です。

https://github.com/rongi/rotate-layout

RotateLayoutの使用は非常に簡単ですし、少ないメモリ消費量は新しいがMatrixを使用してBitmapを回転させるために比較します。

場所あなたImageViewRotateLayout内部のようなRotateLayout .AND ImageView回転さにangleプロパティにExifInterfaceを介して取得し、あなたがビットマップまたは任意の他のもののために心配する必要はありませんで、コード

<com.github.rongi.rotate_layout.layout.RotateLayout 
    android:id="@+id/form3_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:angle="180"> 

    <ImageView 
    android:id="@+id/imageview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 
</com.github.rongi.rotate_layout.layout.RotateLayout> 

とSET orientation値以下ExifInterfaceorientation

あなたは

rotateLayout.setAngle(newAngle); 
+0

こんにちは友人も画像にターゲティングを追加することはできますか?そしてそれは同じように働くだろうか?このメソッドを呼び出す。imageView.setRotation(90); – Devix

+0

@JoseAnguianoはいimageView.setRotation(90)は動作しますが、ビットマップExifの向きが横の場合、90に回転すると、画像ビュー内のビットマップの縦横比は維持されません。 –

0

こんにちは

public Bitmap rotateImage() { 
    Bitmap scaled = null; 
    try { 
     FileInputStream fis = new FileInputStream(file); 
     BitmapFactory.Options bounds = new BitmapFactory.Options(); 
     bounds.inJustDecodeBounds = true; 
     Bitmap bm = BitmapFactory.decodeStream(fis); 

     ExifInterface exif = new ExifInterface(file.getAbsolutePath()); 

     String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
     int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL; 
     int rotationAngle = 0; 
     if (orientation == ExifInterface.ORIENTATION_ROTATE_90) 
      rotationAngle = 90; 
     if (orientation == ExifInterface.ORIENTATION_ROTATE_180) 
      rotationAngle = 180; 
     if (orientation == ExifInterface.ORIENTATION_ROTATE_270) 
      rotationAngle = 270; 
     Matrix matrix = new Matrix(); 
     matrix.setRotate(rotationAngle, (float) bm.getWidth()/2, (float) bm.getHeight()/2); 
     Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); 
     int height = rotatedBitmap.getHeight(), scaledheight, scaledwidth; 
     int width = rotatedBitmap.getWidth(); 
     float aspect; 

     if (width < height) {// portrait 
      aspect = ((float) height/(float) width); 
      scaledwidth = 400; 
      scaledheight = (int) (400 * aspect); 
     } else {// landscape 
      aspect = ((float) width/(float) height); 
      scaledheight = 400; 
      scaledwidth = (int) (400 * aspect); 
     } 
     scaled = Bitmap.createScaledBitmap(rotatedBitmap, scaledwidth, scaledheight, false); 

     File f = new File(getCacheDir(), "scaledBitmap"); 
     f.createNewFile(); 

     Bitmap bitmap = scaled; 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 0 /* ignored for PNG */, bos); 
     byte[] bitmapdata = bos.toByteArray(); 

     FileOutputStream fos = new FileOutputStream(f); 
     fos.write(bitmapdata); 
     fos.flush(); 
     fos.close(); 

     file = f; 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.e("photofile io error", "EXif not done"); 
    } 
    return scaled; 
} 
関連する問題