2014-03-13 57 views
2

我試圖從sdcard相機文件夾加載圖像,加載後我使用縮放位圖創建位圖,因爲我只想顯示圖像的某些部分,因此我嘗試使用createBitmap()創建新的位圖,但它仍然得到模糊,我該如何解決這個問題?如何獲得完全清晰的圖像?

File[] files = targetDirector.listFiles(); 
      for (File file : files) { 

       String path = file.getAbsolutePath(); 
       itemList.add(path); 


       if (myBitmap != null) { 
        myBitmap = null; 
       } 

       myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 

       System.out.println("thumbnails1 width: " + myBitmap.getWidth()); 
       System.out.println("thumbnails1 height: " + myBitmap.getHeight()); 
       myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150, false); 

       ExifInterface exif; 
       try { 
        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) { 
         System.out.println("photopotart"); 
         rotationAngle = 90; 

         System.out.println("myBitmap" + myBitmap.getWidth()); 
         System.out.println("myBitmap" + myBitmap.getHeight()); 

         myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150, 
         false); 



         Matrix matrix = new Matrix(); 
         matrix.postRotate(90); 
         myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, 
           150, 150, matrix, 
           true); 

         myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, 150, 100); 

        myBitmap = highlightImage(myBitmap); 

         photsList.add(myBitmap); 
        } 

        else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
         System.out.println("180 angle"); 
         rotationAngle = 180; 

        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
         System.out.println("other 270"); 
         rotationAngle = 270; 
        } else { 
         System.out.println("photolandscape"); 
        myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150, 
           false); 


         myBitmap = Bitmap.createBitmap(myBitmap, 30, 0, 120, 150); 



         photsList.add(myBitmap); 
        } 

       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 








      } 
     } 

我的XML文件是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="150dip" 
     android:layout_height="150dip" /> 

</LinearLayout> 

回答

1

用於解碼的文件路徑下面的方法。

public void decodeFile(String filePath) { 

    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 

    int scale = 2; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    bmp = BitmapFactory.decodeFile(filePath, o2); // this bmp object of Bitmap is global and you can set it to your ImageView. 
    } 

現在你可以調用這個函數與這一個

myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 

decodeFile(file.getAbsolutePath()); 

您可以根據您的要求,您可以用很讓你的圖片設置你的規模變量替換清晰度良好。

+0

嗨,感謝您使用這個比以前更好的答覆,但出現的問題是一些陰影占據照片 – user3114723

+0

您可以根據您的要求增加REQUIRED_SIZE和縮放變量。所以你可以得到更好的輸出。 – Piyush

+0

目前我正在使用s3 mini和nexus4,然後多少我可以給予REQUIRED_SIZE – user3114723