2014-02-22 28 views
2

在我的Android應用我捕捉人像模式中的照片,但是當一張照片被保存在橫向模式轉換,但我想保存照片肖像模式肖像模式下的照片捕捉,但在風景中的照片aumatic轉換我想在potrait模式android中照片?

CaptureActivity.java

public class CaptureActivity extends Activity 
{ 
    // Activity request codes 
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; 
    public static final int MEDIA_TYPE_IMAGE = 1; 

    // imageview for display captured image 
    ImageView imagecapture; 

    // file url to store image/video 
    public Uri fileUri; 

    // Classes For Database 
    private  SQLiteDatabase mSQLiteDatabase = null; 
    private  DB_Helper  mDB_Helper = null; 

    public static ContextWrapper contextWrapper; 


    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.capturephoto); 

     imagecapture=(ImageView)findViewById(R.id.imagecapture); 
     contextWrapper = new ContextWrapper(getApplicationContext()); 

     // Checking camera availability 
     if(!isDeviceSupportCamera()) 
     { 
      Toast.makeText(getApplicationContext(),"Sorry! Your device doesn't support camera", Toast.LENGTH_LONG).show(); 
      // will close the app if the device does't have camera 
      finish(); 
     } 
     else 
     { 
      captureImage(); 
     } 

    } 

    /* Opening DB */ 
    private void Open_Database() 
    { 
     mDB_Helper = new DB_Helper(this); 
     mSQLiteDatabase = mDB_Helper.getWritableDatabase(); 
    } 

    /* Closing DB */ 
    private void Close_Database() 
    { 
     if (mSQLiteDatabase != null && mDB_Helper != null) { 
      mSQLiteDatabase.close(); 
      mDB_Helper.close(); 
     } 
    } 

    /** 
    * Checking device has camera hardware or not 
    * */ 
    private boolean isDeviceSupportCamera() 
    { 
     if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) 
     { 
      // this device has a camera 
      return true; 
     } 
     else 
     { 
      // no camera on this device 
      return false; 
     } 
    } 

    /** 
    * Capturing Camera Image will lauch camera app requrest image capture 
    */ 
    private void captureImage() 
    { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri); 
      // start the image capture Intent 
     startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
    } 

    /** 
    * Here we store the file url as it will be null after returning from camera 
    * app 
    */ 

    @Override 
    protected void onSaveInstanceState(Bundle outState) 
    { 
     super.onSaveInstanceState(outState); 

     // save file url in bundle as it will be null on scren orientation 
     // changes 
     outState.putParcelable("file_uri", fileUri); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) 
    { 
     super.onRestoreInstanceState(savedInstanceState); 

     // get the file url 
     fileUri = savedInstanceState.getParcelable("file_uri"); 
    } 

    /** 
    * Receiving activity result method will be called after closing the camera 
    * 
    * */ 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     // if the result is capturing Image 
     if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) 
     { 
      if (resultCode == RESULT_OK) 
      { 
       // successfully captured the image 
       // display it in image view 
       previewCapturedImage(); 
      } 
      else if (resultCode == RESULT_CANCELED) 
      { 
       // user cancelled Image capture 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } 
      else 
      { 
       // failed to capture image 
       Toast.makeText(getApplicationContext(),"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }  



    /** 
    * Display image from a path to ImageView 
    */ 
    private void previewCapturedImage() 
    { 
     try 
     {   

      // for open database 
      Open_Database(); 

      Bitmap bitmap; 
      try { 
       bitmap = getImage(fileUri.getPath(),getApplicationContext()); 
       imagecapture.setImageBitmap(bitmap); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
      Date myDate = new Date(); 
      String realdatetime = dateformat.format(myDate); 
      Log.e("Current Date","------>"+realdatetime); 

      mDB_Helper.Insert_MYPHOTOS_Table(mSQLiteDatabase,"title",realdatetime, "description",fileUri.getPath()); 

      // for close database 
      Close_Database(); 

     } 
     catch (NullPointerException e) 
     { 
      e.printStackTrace(); 
     } 

    } 


    public Bitmap getImage(String path,Context con) throws IOException 
    { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 
     int srcWidth = options.outWidth; 
     int srcHeight = options.outHeight; 
     int[] newWH = new int[2]; 
     newWH[0] = srcWidth/2; 
     newWH[1] = (newWH[0]*srcHeight)/srcWidth; 

     int inSampleSize = 1; 
     while(srcWidth/2 >= newWH[0]){ 
      srcWidth /= 2; 
      srcHeight /= 2; 
      inSampleSize *= 2; 
     } 

     options.inJustDecodeBounds = false; 
     options.inDither = false; 
     options.inSampleSize = inSampleSize; 
     options.inScaled = false; 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options); 
     ExifInterface exif = new ExifInterface(path); 
     String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
     System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s); 
     Matrix matrix = new Matrix(); 
     float rotation = rotationForImage(con, Uri.fromFile(new File(path))); 
     if (rotation != 0f) { 
      matrix.preRotate(rotation); 
     } 

     Bitmap pqr=Bitmap.createBitmap(
       sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true); 


     return pqr; 
    } 


    public float rotationForImage(Context context, Uri uri) { 
     if (uri.getScheme().equals("content")) { 
      String[] projection = { Images.ImageColumns.ORIENTATION }; 
      Cursor c = context.getContentResolver().query(
        uri, projection, null, null, null); 
      if (c.moveToFirst()) { 
       return c.getInt(0); 
      } 
     } else if (uri.getScheme().equals("file")) { 
      try { 
       ExifInterface exif = new ExifInterface(uri.getPath()); 
       int rotation = (int)exifOrientationToDegrees(
         exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
           ExifInterface.ORIENTATION_NORMAL)); 
       return rotation; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      }catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
     return 0f; 
    } 

    private static float exifOrientationToDegrees(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; 
    } 

    /** 
    * ------------ Helper Methods ---------------------- 
    * */ 

    /** 
    * Creating file uri to store image/video 
    */ 
    public Uri getOutputMediaFileUri(int type) 
    { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /** 
    * returning image/video 
    */ 
    private static File getOutputMediaFile(int type) 
    { 

      File mediaFile = null; 

      Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
      if(isSDPresent) 
      { 
       File directory= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Mymemoryphotos"); 

       if(!directory.exists()) 
       { 
        Log.e("Create External Directory","------>"); 
        directory.mkdirs(); 
       } 

       // Create a media file name 
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date()); 


       if (type == MEDIA_TYPE_IMAGE) 
       { 
        mediaFile = new File(directory.getPath() + File.separator + "IMG_" + timeStamp + ".JPEG"); 
       } 
       else 
       { 

        return null; 

       } 

      } 
      else 
      { 

       File directory = contextWrapper.getDir("Mymemoryphotos", Context.MODE_PRIVATE); 

       if(!directory.exists()) 
       { 
        Log.e("Create Internal Directory","------>"); 
        directory.mkdirs(); 

       } 

       // Create a media file name 
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());    

       if (type == MEDIA_TYPE_IMAGE) 
       { 
        mediaFile = new File(directory.getPath() + File.separator + "IMG_" + timeStamp + ".JPEG"); 
       } 
       else 
       { 

        return null; 

       } 


      } 

     return mediaFile; 
    } 

} 

我想造成這樣的照片時,保存在SD卡

enter image description here

我現在的結果時potrait模式,但照片自動 捕捉圖像轉換爲橫向模式

enter image description here

回答

0

使用ExifInterface閱讀,當你表現出來&旋轉位圖的照片EXIF標籤。您可以檢查this一些示例代碼,但它需要編輯&清洗了一下

+0

哎,當我告訴在imageview的形象已經與potrait做,但我想,當我被保存在SD卡旋轉圖像有可能 – Mahesh

+0

我不理解你。請嘗試更好地構造句子 – Sam

+0

先看到我在縱向模式下捕捉圖像比我在SD卡上保存該圖像,但此圖像保存在橫向模式下而不是縱向模式下圖像自動旋轉爲橫向模式 – Mahesh

相關問題