2013-07-04 18 views
3

我正在做一個應用程序,它使用相機拍攝照片,然後旋轉並縮放它。 我需要旋轉圖像,因爲相機返回錯誤的旋轉圖像,我需要縮放它以減小其大小。 我首先將臨時目錄保存在相機返回的原始圖像中,然後我讀取它並進行修改,將新圖像保存到新文件中。 我嘗試過使用矩陣來旋轉和縮放圖片,但它失去了質量。 然後我試着用Bitmap.createScaledBitmap先縮放它,然後用矩陣旋轉它,但結果甚至比僅使用矩陣的結果更醜。 然後我試着先旋轉它,然後使用總是Bitmap.createScaledBitmap來調整它的大小。圖像不會丟失質量,但是在旋轉圖像後縮放它並將寬度和高度反轉後,圖像就會被拉伸。也嘗試根據所做的旋轉反轉高度和寬度,但它又失去了質量。 這是我寫的最後一段代碼:Android - 縮放,旋轉並將位圖保存到SD卡而不會丟失質量

in= new FileInputStream(tempDir+"/"+photo1_path); 
      out = new FileOutputStream(file+"/picture.png"); 
      Bitmap src = BitmapFactory.decodeStream(in); 
      int iwidth = src.getWidth(); 
      int iheight = src.getHeight(); 
      int newWidth = 0; 
      int newHeight = 0; 

       newWidth = 800; 
       newHeight = 600; 

       // calculate the scale - in this case = 0.4f 
       float scaleWidth = ((float) newWidth)/iwidth; 
       float scaleHeight = ((float) newHeight)/iheight; 

       // createa matrix for the manipulation 
       Matrix matrix = new Matrix(); 
       // resize the bit map 
       //matrix.postScale(scaleWidth, scaleHeight); 
       int orientation = getOrientation(MyActivity.this,Uri.parse(tempDir+"/"+photo1_path)); 
       switch(orientation) { 
       case 3: 
        orientation = 180; 
        break; 
       case 6: 
        orientation = 90; 
        break; 
       case 8: 
        orientation = 270; 
        break; 
       } 
       int rotate = 0; 
       switch(orientation) { 
       case 90: 
        rotate=90; 
        break; 
       case 180: 
        rotate=180; 
        break; 
       case 270: 
        rotate=270; 
        break; 

       } 
       // rotate the Bitmap 
       matrix.postRotate(rotate); 

       src =Bitmap.createScaledBitmap(src , newWidth, newHeight, false); 
       // recreate the new Bitmap 
       Bitmap new_bit = Bitmap.createBitmap(src, 0, 0, 
           src.getWidth(), src.getHeight(), matrix, true); 
         new_bit.compress(Bitmap.CompressFormat.PNG, 100, out); 

有什麼建議嗎?

編輯:如果我只旋轉或只縮放圖像,它不會失去質量。這是當我做這兩個圖像失去質量。另外,如果我在調整大小和縮放大小後將圖像放入ImageView中,它不會丟失質量,只是在將它保存到文件時丟失了質量。

回答

4

解決! 我使用BitmapFactory inSampleSize選項調整圖像大小,圖像不會丟失質量。 代碼:

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
bmpFactoryOptions.inJustDecodeBounds = true; 
Bitmap bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path , bmpFactoryOptions); 


int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)600); 
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)800); 

if (heightRatio > 1 || widthRatio > 1) 
{ 
    if (heightRatio > widthRatio){ 
     bmpFactoryOptions.inSampleSize = heightRatio; 
    } else { 
     bmpFactoryOptions.inSampleSize = widthRatio; 
    } 
} 

bmpFactoryOptions.inJustDecodeBounds = false; 
bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path, bmpFactoryOptions); 
//bm.compress(Bitmap.CompressFormat.PNG, 100, out); 
/*Bitmap new_bit = Bitmap.createScaledBitmap(src , newWidth, newHeight, true); 
      new_bit.compress(Bitmap.CompressFormat.PNG, 100, out);*/ 


// recreate the new Bitmap 
src = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(), bm.getHeight(), matrix, true); 


//Bitmap dest = Bitmap.createScaledBitmap(new_bit , newWidth, newHeight, true); 
src.compress(Bitmap.CompressFormat.PNG, 100, out); 
+0

問題與'Bitmap.CompressFormat.PNG'就是文件每次旋轉時都會變大。 –

0

使用這種方法將旋轉位圖...

private Bitmap checkifImageRotated() { 
    ExifInterface exif; 
    try { 
     exif = new ExifInterface(file.getAbsolutePath()); 
     int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
     int rotate = 0; 
     switch (orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_270 : 
       rotate = -90; 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_180 : 
       rotate = 180; 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_90 : 
       rotate = 90; 
       break; 
     } 
     if (rotate != 0) { 
      Bitmap bitmap = BitmapFactory.decodeFile(getTempFile().getPath()); 
      Matrix matrix = new Matrix(); 
      matrix.setRotate(rotate); 
      Bitmap bmpRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); 
      recycle(bitmap); 
      return bmpRotated; 
     } 
    } 
    catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 
+0

謝謝,我剛剛嘗試過,但沒有變化,它總是失去質量。無論如何,你也使用矩陣來旋轉圖像,所以看起來似乎沒有什麼變化,即使用ExifInterface得到方向:( – dany84

0

不要讓新的位圖只覆蓋

INT角度= 0; int valueangle = 0;位圖位圖;

 @Override 
     public void onClick(View v) { 

      System.out.println("valueeeeeee " + angle); 
      if (bitmap != null) { 

       angle = valueangle + 90; 



       Matrix matrix = new Matrix(); 
       matrix.postRotate(angle); 

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



       main_img.setImageBitmap(bitmap); 

      } else { 

       System.out.println(" bitmap is null"); 
      } 

     } 
+0

謝謝,我剛剛試圖重寫,但結果是一樣的,圖像質量下降,如果這個代碼是幫助完全接受和upvote – dany84

+0

意味着改變什麼解釋得當, –

+0

這個代碼沒有解決我的問題,我不能接受它,對不起,我可以改變一些東西嗎? – dany84

相關問題