2013-01-11 117 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的值太大,你会过多地对图片进行下采样。