提高布局中的图像质量

时间:2013-01-11 10:29:43

标签: android android-layout android-image

我想用我的相机或我的图片galery放置一张图片,但是质量太低,你可以在这些图片中看到:

原始图片:

Real image

enter image description here

enter image description here

1.-真实图像

2. - 直接从相机拍摄此图像

3.从图像库中拍摄此图像

特别是在第3张照片中,2张和3张照片的质量非常差,但我也希望提高第2张照片的质量。有没有办法实现呢?

以下是我用来拍照的代码(来自相机和图片库):

   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 个答案:

答案 0 :(得分:0)

我认为你在o2.inSampleSize = scale中使用的var scale的值太大了。你对这个图片进行了过多的下采样。<​​/ p>