1

when i capture image from mobile camera and save it to local storage its quality is fine but when i get this image in my android app image quality to much down even unable to read text in the picture.how i get image in application in android app without losing the quality of picture.

First Activity

    public class MainActivity extends AppCompatActivity {
    static final int REQUEST_IMAGE_CAPTURE = 1;
    ImageButton imageButton;

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

        imageButton = findViewById(R.id.camera);

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(takePictureIntent.resolveActivity(getPackageManager()) != null){
                    startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
                }
            }
        });

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {

            Bitmap photo = (Bitmap) data.getExtras().get("data");


            //ByteArrayOutputStream stream = new ByteArrayOutputStream();

            //photo.compress(Bitmap.CompressFormat.PNG,100 , stream);

            //byte[] byteArray = stream.toByteArray();

            Intent i = new Intent(MainActivity.this,PrintActivity.class);
            i.putExtra("image",photo);
            startActivity(i);
        }
    }
}

Second Activity

public class PrintActivity extends AppCompatActivity {
ImageView imageView;
Bitmap bmp;


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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    imageView = findViewById(R.id.imageViewer);

   // byte[] byteArray = getIntent().getByteArrayExtra("image");
    //bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    bmp = getIntent().getParcelableExtra("image");

    imageView.setImageBitmap(bmp);

}

public void btnOnClickPrint(View v){
    PrintHelper printHelper = new PrintHelper(this);
    printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
    printHelper.printBitmap("Image Print",bmp);
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hamza Shahid Ali
  • 224
  • 4
  • 14
  • Possible duplicate of [Low picture/image quality when capture from camera](https://stackoverflow.com/questions/10377783/low-picture-image-quality-when-capture-from-camera) – krishank Tripathi Mar 10 '18 at 09:55

2 Answers2

0
Bitmap photo = (Bitmap) data.getExtras().get("data");

That is only a thumbnail of the original picture.

So what ever you do it will stay a thumbnail.

You should use that camera intent differently. Indicate where the camera app should save the full picture (Use EXTRA_STREAM) and then tell that path to the second activity so it can load the original picture.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

You have to set EXTRA_OUTPUT in intent

cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);

image uri is a uri where after the intent for get the a picture finishes, the picture then is stored in that uri, in the following code is named imageUri and is defined gloablly so you can access it in the onActivityResult

this code worked for me

void takePicture(){
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Date dat = Calendar.getInstance().getTime();
        SimpleDateFormat simpleDate =  new SimpleDateFormat("yyyy-mm-dd-hh:mm:ss");
        String nameFoto = simpleDate.format(dat) + ".png";
        String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ File.separator +nameFoto;
        File ff = new File(filename);

        try {
            ff.createNewFile();
            imageUri = FileProvider.getUriForFile(IngresarFactura.this, BuildConfig.APPLICATION_ID + ".provider",ff);
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);

            //COMPATIBILITY
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
                cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            } else {
                List<ResolveInfo> resInfoList = IngresarFactura.this.getPackageManager().queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    yourintent.this.grantUriPermission(packageName, imageUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
            //COMPATIBILITY
            activityResultLaunch.launch(cameraIntent);
        } catch (Exception e) {
            Toast.makeText(IngresarFactura.this, "Ocurrio un error  al tomar la foto.", Toast.LENGTH_LONG).show();
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionAsString = sw.toString();
            mensaje.setText(ff.getPath() + exceptionAsString);
        }
    }

i also do what is stated here

https://stackoverflow.com/a/45751453/16645882

and then it worked

here is code for the activityResultLaunch because startActivityForResult is depreceated

activityResultLaunch = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if ( result.getResultCode() == Activity.RESULT_OK) {
                            try {
                                Bitmap photo = MediaStore.Images.Media.getBitmap(IngresarFactura.this.getContentResolver(), imageUri);
//here this is gow you get the bitmap
                            } catch (Exception e) {
                               
                            }
                        }
                        else{
                            
                        }
                    }
                });

and define this at start of your java document as

ActivityResultLauncher<Intent> activityResultLaunch;