Amir Sasani
Amir Sasani

Reputation: 327

bitmap bad quality after set to imageview

I am creating a app that opens camera for user and after image captured it will be shows on ImageView! But the image in ImageView has very bad quality
here is the code:

public class Camera extends AppCompatActivity implements View.OnClickListener {

ImageView imgView;
Button camera, setBackground;

Intent i;

int cameraData = 0;

Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);
    initialize();
}

private void initialize() {
    imgView = (ImageView) findViewById(R.id.bPicture);
    camera = (Button) findViewById(R.id.bOpenCamera);
    setBackground = (Button) findViewById(R.id.bSetBackground);

    camera.setOnClickListener(this);
    setBackground.setOnClickListener(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        Bundle extras = data.getExtras();
        bitmap = (Bitmap) extras.get("data");

        Bitmap resizedBmp = Bitmap.createScaledBitmap(bitmap, imgView.getWidth(),imgView.getHeight(), false);
        imgView.setImageBitmap(resizedBmp);
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.bOpenCamera:
            i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameraData);
            break;
        case R.id.bSetBackground:
            try {
                WallpaperManager wallmngr = WallpaperManager.getInstance(this);
                wallmngr.setBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
    }
}

}

what should I do to increase image quality?

Upvotes: 0

Views: 2958

Answers (2)

OBX
OBX

Reputation: 6114

This is how I made it work for me! Assuming you are capturing an image, and would like to show the captured image in a new Activity, you can follow this way:

First on the click of button you can:

 public void cameraFuture(View view) // <-- onClick() method of Camera Button
{
    Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(Environment.getExternalStorageDirectory(),
            "MyPhoto.jpg");
    outPutfileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
    startActivityForResult(intent, TAKE_PIC);
}

Then on the onActivityResult() method, you can do this way:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PIC && resultCode==RESULT_OK){
        Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
        
        Intent bitIntent = new Intent(this, CameraTake.class);
        bitIntent.putExtra("imageUri", outPutfileUri);
        startActivity(bitIntent);
        finish();

      
    }

}

And in the next Activity, you can receive the file this way:

Intent receiveIntent = getIntent();

    uri = receiveIntent.getParcelableExtra("imageUri");


    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            receiveImg= null;
        } else {
            receiveImg= extras.getString("PASSER");
        }
    } else {
        receiveImg= (String) savedInstanceState.getSerializable("PASSER");
    }




    File imgFile = new  File(Environment.getExternalStorageDirectory(),"MyPhoto.jpg");

    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ImageView myImage = (ImageView) findViewById(R.id.camOut);

        myImage.setImageBitmap(myBitmap);

    }

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006574

Use something other than the thumbnail. Quoting the documentation for ACTION_IMAGE_CAPTURE, with emphasis added:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

So, specify a Uri in EXTRA_OUTPUT where a full camera image should be written to. Then, use an image-loading library, like Picasso, to load the photo into your ImageView.

Here is a sample app that demonstrates using EXTRA_OUTPUT.

Upvotes: 3

Related Questions