0

While I select the image from the gallery and shows it in ImageView. The image quality is all right. But, uploading an image on the server, it lost quality and become a blur. I obtain the image from the camera by this code.

private void onCaptureImageResult(Intent data) {
    bitmap = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    File destination = new File(
        Environment.getExternalStorageDirectory(),
        System.currentTimeMillis() + ".jpg"
    );

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    imageView.setImageBitmap(bitmap);
}

Then, I did this work-

private String imageToString(Bitmap bitmap){
    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
    byte[] imgByte=byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(imgByte,Base64.DEFAULT);
}

and used this function to compress my selected photo. But, it makes the loss of that image quality and image become a blur on the server. Why am I facing this problem?

Yogesh Aggarwal
  • 1,071
  • 2
  • 12
  • 30

2 Answers2

0

You could use the .png format as it's lossless and doesn't reduce the image quality. On the other hand the .jpeg format is just the opposite of this.

private String imageToString(Bitmap bitmap){
    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] imgByte=byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(imgByte, Base64.DEFAULT);
}
Yogesh Aggarwal
  • 1,071
  • 2
  • 12
  • 30
0

You did not show the intent to start a Camera app.

But you did it in such a way that you only got a thumbnail of the picture taken.

Change the intent. Add an uri where the camera app can save the full picture.

There are 783 examples of such an intent on stackoverflow and even more on the internet.

blackapps
  • 8,011
  • 2
  • 11
  • 25