2017-02-15 64 views
1

我有一種方法來拍攝照片,像我想要的那樣拍攝垂直圖像的樣本,水平圈並保存垂直照片,就像我希望進行的練習一樣,Activity稱爲ViewerActivity,它是這裏問題在ImageView中顯示水平樣本的時刻開始:/他正在讀取,ExifInterface類允許控制圖像的方向,但是在顯示圖像時仍然是水平的onActivityResult與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

試試這個:

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);` 

其中file是圖像文件的名稱。

+0

你好,但有出現我對於這種方法的其他疑問,可以實現拍攝照片時的部分,還是已經發送給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像下面的代碼

<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> 

,並設置orientation值是通過ExifInterface進入angle財產RotateLayout。而ImageView旋轉,你不必擔心爲位圖或其他任何東西爲ExifInterfaceorientation

您可以從Java代碼中動態設置的角度值,爲RotateLayout對象像下面

rotateLayout.setAngle(newAngle); 
+0

您好朋友還可以爲圖片添加定位嗎?它會一樣嗎?用這個方法來設置imageView.setRotation(90); – Devix

+0

@JoseAnguiano yes 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; 
}