2013-01-11 158 views
0

我希望把我從我的相機或從我的圖片galery佈局中的拍攝影像,但質量是如此之低,你可以在這些圖片中看到:提高圖像質量在佈局

原始圖片:

Real image

enter image description here

enter image description here

1.實時IMA GE

2. - 以直接從相機這個圖像

3. - 以從圖像庫

質量這個圖像是在2頁3的圖片如此差特意在第三之一,但我也想提高第二名的質量。有什麼辦法可以做到嗎?

這裏是我用來拍攝照片(從相機,從圖像庫)的代碼:

public class PhotoEditingActivity extends Activity { 


private byte[] photoByteArray; 
private Button btnRotate; 
private ImageView ivPhoto; 
private Bitmap bmp; 
private Button btnEditDone; 
private ChooseImageInputDialog dialogBuilder; 
private AlertDialog dialog; 
private Button btnCancel; 
private Button btnChooseNewPhoto; 
private String actualPath; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.photo_editing); 
    setUpViews(); 
    /*try{ 
     getPhoto(); 
    }catch(Exception e){ 
     showOptionsDialog(); 
    }*/ 
    showOptionsDialog(); 
    //first try to get photo from intent, 
    //if there is no photo 
    //show options to take one 
} 

private void showOptionsDialog() { 
    dialogBuilder = new ChooseImageInputDialog(this); 
    dialog = dialogBuilder.create(); 
    dialog.show(); 

} 

/*private void getPhoto() { 
    Intent i = this.getIntent(); 
    Bundle extras = i.getExtras(); 
    photoByteArray = extras.getByteArray("photo"); 
    bmp = BitmapFactory.decodeByteArray(photoByteArray, 0, photoByteArray.length); 
    ivPhoto.setImageBitmap(bmp); 

}*/ 

private void setUpViews() { 
    ivPhoto = (ImageView)findViewById(R.id.ivPhotoEditing); 
    btnRotate = (Button)findViewById(R.id.btnPhotoEditRotate90); 
    btnRotate.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Matrix matrix = new Matrix(); 
      matrix.postRotate(90); 
      bmp = Bitmap.createBitmap(bmp, 0, 0, 
              bmp.getWidth(), bmp.getHeight(), 
              matrix, true); 

      ivPhoto.setImageBitmap(bmp); 

     } 
    }); 
    btnCancel = (Button)findViewById(R.id.btnPhotoEditingCancel); 
    btnCancel.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      PhotoEditingActivity.this.setResult(RESULT_CANCELED); 
      PhotoEditingActivity.this.finish(); 
     } 
    }); 

    btnChooseNewPhoto =(Button)findViewById(R.id.btnPhotoEditingTakeNewPicture); 
    btnChooseNewPhoto.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      showOptionsDialog(); 

     } 
    }); 

    btnEditDone = (Button)findViewById(R.id.btnPhotoEditDone); 
    btnEditDone.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View arg0) { 
      if(photoByteArray!=null){ 
       ByteArrayOutputStream os = new ByteArrayOutputStream(); 
       bmp.compress(Bitmap.CompressFormat.PNG, 100, os); 
       photoByteArray = os.toByteArray(); 
       Intent i = PhotoEditingActivity.this.getIntent(); 
       BitmapData.setBitmap(bmp); 
       Log.d("actual path pea", actualPath); 
       PhotoEditingActivity.this.setResult(RESULT_OK, i); 
       PhotoEditingActivity.this.finish(); 
      }else{ 
       Toast t = Toast.makeText(PhotoEditingActivity.this, "Please take a picture first", Toast.LENGTH_SHORT); 
       t.show(); 
      } 

     } 
    }); 
} 

public String getRealPathFromURI(Uri contentUri) { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

private Bitmap decodeFile(File f){ 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

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

     //Find the correct scale value. It should be the power of 2. 
     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; 
     } 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode) { 
    case ChooseImageInputDialog.ACTIVITY_SELECT_PHOTO: 
     if(resultCode == RESULT_OK){ 
      Log.d("resultcode pea pick", "result ok"); 
      Uri selectedImageUri=data.getData(); 
      actualPath=getRealPathFromURI(selectedImageUri); 
      File file=new File(actualPath); 
      bmp=decodeFile(file); //this is new bitmap which you can use for your purpose 

      ByteArrayOutputStream os = new ByteArrayOutputStream(); 
      //bmp.compress(Bitmap.CompressFormat.PNG, 65, os); 
      photoByteArray = os.toByteArray(); 
      ivPhoto.setImageBitmap(bmp); 
     }else{ 
      Log.d("resultcode pea pick", "result not ok"); 
     } 
     break; 
    case ChooseImageInputDialog.ACTIVITY_CAMERA_PHOTO: 
     if(resultCode == RESULT_OK){ 
      Bundle extras = data.getExtras(); 
      bmp = (Bitmap)extras.get("data"); 
      ByteArrayOutputStream os = new ByteArrayOutputStream(); 
      //bmp.compress(Bitmap.CompressFormat.PNG, 65, os); 
      photoByteArray = os.toByteArray(); 
      ivPhoto.setImageBitmap(bmp); 
      actualPath = getRealPathFromURI(data.getData()); 
      Log.d("take pic actual path", actualPath); 

     } 
     break; 
    } 
} 
    } 

把它在佈局中的代碼:

private void setUpViews() { 
    // TODO Auto-generated method stub 
    upSize = up.length; 
    upCount = 1; 
    downSize = down.length; 
    downCount = 1; 
    head = (ImageView)findViewById(R.id.galleryUp); 
    tShirt = (ImageView)findViewById(R.id.galleryDown); 
    rowUp1 = (ImageView)findViewById(R.id.ivMonkeyRowUp1); 
    rowUp2 = (ImageView)findViewById(R.id.ivMonkeyRowUp2); 
    rowDown1 = (ImageView)findViewById(R.id.ivMonkeyRowDown1); 
    rowDown2 = (ImageView)findViewById(R.id.ivMonkeyRowDown2); 
    llTab = (LinearLayout)findViewById(R.id.llTab); 
    llTop = (LinearLayout)findViewById(R.id.llTop); 
    monkeyTab = (LinearLayout)findViewById(R.id.llTab1); 
    monkeyTab.setOnClickListener(this); 
    shareTab = (LinearLayout)findViewById(R.id.llTab3); 
    shareTab.setOnClickListener(this); 
    web = (ImageView)findViewById(R.id.imageWeb); 
    web.setOnClickListener(this); 
    ll = (LinearLayout) findViewById(R.id.llPhoto); 
    bmp = BitmapData.getBitmap(); 
    d =(Drawable) new BitmapDrawable (getResources(), bmp); 
    ll.setBackgroundDrawable(d); 

    head2 = (ImageView)findViewById(R.id.galleryUp2); 
    tShirt2 = (ImageView)findViewById(R.id.galleryDown2); 
    rowUp12 = (ImageView)findViewById(R.id.ivMonkeyRowUp12); 
    rowUp22 = (ImageView)findViewById(R.id.ivMonkeyRowUp22); 
    rowDown12 = (ImageView)findViewById(R.id.ivMonkeyRowDown12); 
    rowDown22 = (ImageView)findViewById(R.id.ivMonkeyRowDown22); 
    llPhoto2 = (LinearLayout) findViewById(R.id.llPhoto2); 


} 
+1

我認爲您在o2.inSampleSize = scale中使用的var scale的值太大,您會過多地對圖片進行下采樣。 – VinceFR

+0

謝謝!它適用於圖庫圖像。對於來自相機的照片,我必須稍微改變一下,並將其作爲圖庫圖像,現在都具有很高的質量。 –

+0

太棒了!我發表評論作爲答案,請你接受嗎? – VinceFR

回答

0

我認爲你在o2.inSampleSize = scale中使用的var scale的值太大,你會過多地對圖片進行下采樣。