2014-06-06 24 views
2

我想採用Android 機攝像頭到SD卡旋轉保存我捕獲的圖像。由於本機攝像機默認爲風景。但我不想使用位圖。是否有可能這樣做?請幫忙。我是android開發新手。旋轉和保存拍攝的圖像,而無需使用位圖

當我的圖像旋轉90度時,我需要在保存前使用位圖將其旋轉。 但在某些設備上bmp即將到來null

相機活動佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <FrameLayout 
     android:id="@+id/camera_preview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    /> 
</RelativeLayout> 

manifest資源配置文件

<activity 
    android:name="com.example.androiddms.CameraActivity" 
    android:configChanges="keyboardHidden|orientation" 
    android:label="@string/title_activity_camera" 
    android:screenOrientation="landscape" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 
</activity> 

相機Activity.java

FileOutputStream fos = new FileOutputStream(pictureFile); 
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length); 
Matrix matrix = new Matrix(); 
matrix.postRotate(90); 

bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
bmp.compress(Bitmap.CompressFormat.JPEG, 100,fos); 
+0

你用位圖面臨什麼問題? –

+0

位圖使用了很多內存。儘管存在一些技術來處理它們。但我只是想嘗試如果我可以保存**捕獲的圖像**,而不使用**位圖**。 – ZZeyaNN

回答

0

從相機拍攝照片後,您應該撥打onActivityResult並在ImageView上顯示拍攝的照片。 Bitmap需要記憶,但你應該處理它。從它開放camera Intent和捕捉圖像:按照以下希望下面的步驟解決您的問題

STEP 1

private static final int REQUEST_CAMERA = 0; 

Bitmap bmp; 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

startActivityForResult(intent, REQUEST_CAMERA); 

STEP 2:override onActivityResult方法

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 

     if(resultCode == RESULT_OK) 
     { 
      if(requestCode == REQUEST_CAMERA) 
      {    
       Uri uri = (Uri) data.getData(); 

       try 
       { 
        bmp = decodeUri(uri); 

        your_image_view.setImageBitmap(bmp); 
       } 
       catch (FileNotFoundException e) 
       { 
        e.printStackTrace(); 
       } 
      } 

     } 
    } 

STEP 3 :複製下面的方法,並將其粘貼到您的Activity

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException 
    { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 

     o.inJustDecodeBounds = true; 

     BitmapFactory.decodeStream(
       getContentResolver().openInputStream(selectedImage), null, o); 

     final int REQUIRED_SIZE = 70; 

     int width_tmp = o.outWidth, height_tmp = o.outHeight; 

     int scale = 1; 

     while (true) 
     { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) 
      { 
       break; 
      } 
      width_tmp /= 2; 

      height_tmp /= 2; 

      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 

     o2.inSampleSize = scale; 

     return BitmapFactory.decodeStream(
       getContentResolver().openInputStream(selectedImage), null, o2); 
    } 

並且按照以下步驟完成後,您將能夠從相機捕捉圖像並將其設置爲您的圖像視圖而不會出現內存錯誤。希望這會幫助你。

相關問題