2012-11-08 110 views
2

在我的應用程序中,我通過攝像頭捕獲圖像並將其路徑保存在字符串變量(用於在服務器上發送圖像的SD卡路徑),同時我還在ImageView上設置了該圖像。但圖像會自動旋轉爲橫向,而不是以直角設置。我一直在尋找在計算器上和谷歌找到EXIF旋轉使用:爲什麼圖像在被相機捕捉時會旋轉?

http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/

,但它無法正常工作。我的代碼是: (後croping操作碼是onActivityResult的)

case AppConstants.CROP_FROM_CAMERA: 
     if (data != null) { 
      Bundle extras = data.getExtras(); 

      if (extras != null) { 
       Bitmap photo = extras.getParcelable("data"); 
       File file = new File("/sdcard/bidnear/"); 
       if (!file.isDirectory()) 
        file.mkdir(); 
       imageUrl = "/sdcard/bidnear/thumbimgcrop.png"; 
       file = new File("/sdcard/bidnear/thumbimgcrop.png"); 
       try { 
        photo = rotateImage(photo,mImageCaptureUri); 
        photo.compress(Bitmap.CompressFormat.PNG, 100, 
          new FileOutputStream(file)); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       objimg.setBackgroundResource(0); 
       objimg.setImageBitmap(photo); 

      } 
     } 

旋轉的方法是:

private Bitmap rotateImage(Bitmap objbitmap,Uri uri) 
{ 
    Matrix matrix = new Matrix(); 
    float rotation =rotationForImage(MyProfile.this, uri); 
    if (rotation != 0f) { 
     matrix.preRotate(rotation); 
    } 

    Bitmap resizedBitmap = Bitmap.createBitmap(
      objbitmap, 0, 0,80,80, matrix, true); 
    return resizedBitmap; 
} 

它不工作;我打開相機和拍攝代碼:

private void setUserImage() { 

    final String[] objimagechooseoptions = new String[] { 
      AppConstants.SELECT_CAMERA, AppConstants.SELECT_GALLERY }; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.select_dialog_item, objimagechooseoptions); 
    AlertDialog.Builder objbuilder = new AlertDialog.Builder(this); 
    objbuilder.setTitle("Select Image"); 

    objbuilder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { // pick from 
                   // camera 
      if (item == 0) { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       mImageCaptureUri = Uri.fromFile(new File(Environment 
         .getExternalStorageDirectory(), "tmp_avatar_" 
         + String.valueOf(System.currentTimeMillis()) 
         + ".png")); 
       intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
         mImageCaptureUri); 
       try { 
        intent.putExtra("return-data", true); 
        startActivityForResult(intent, 
          AppConstants.PICK_FROM_CAMERA); 
       } catch (ActivityNotFoundException e) { 
        e.printStackTrace(); 
       } 
      } else { // pick from file 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, 
         "Complete action using"), 
         AppConstants.PICK_FROM_FILE); 
      } 
     } 
    }); 
    final AlertDialog dialog = objbuilder.create(); 
    dialog.show(); 
} 

回答

0

點擊this或與此

case AppConstants.CROP_FROM_CAMERA: 
    if (data != null) { 
     Bundle extras = data.getExtras(); 

     if (extras != null) { 
      Bitmap photo = extras.getParcelable("data"); 
      File file = new File("/sdcard/bidnear/"); 
      if (!file.isDirectory()) 
       file.mkdir(); 
      imageUrl = "/sdcard/bidnear/thumbimgcrop.png"; 
      file = new File("/sdcard/bidnear/thumbimgcrop.png"); 


    try { 

    ExifInterface exif = new ExifInterface(filePath); 
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
    //Toast.makeText(getApplicationContext(), ""+orientation, 1).show(); 
    Log.v("log", "ort is "+orientation); 

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


    if (orientation==6) 
     { 
        Matrix matrix = new Matrix(); 
        matrix.postRotate(90); 
        photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true); 
       } 
       else if (orientation==8) 
       { 
        Matrix matrix = new Matrix(); 
        matrix.postRotate(270); 
        photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true); 
       } 

       else if (orientation==3) 
       { 
        Matrix matrix = new Matrix(); 
        matrix.postRotate(180); 
        photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true); 
       } 
       Log.v("log", "before width is "+photo.getWidth() + " and height is "+photo.getHeight()); 


      objimg.setBackgroundResource(0); 
      objimg.setImageBitmap(photo); 

     } 
    } 
嘗試
相關問題