Save Image with Better Resolution

Asked

Viewed 558 times

2

Hello, I am saving images in the database. But the resolution is very bad. I am using the following:

btncamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tirarfoto();

        }
    });

    btngaleria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galeriaIntent = new Intent(Intent.ACTION_GET_CONTENT);
            galeriaIntent.setType("image/*");

            startActivityForResult(galeriaIntent, ESCOLHERFOTO);


        }
    });

Elsewhere I use this:

 if (foto != null) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    foto.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte[] bytearray = stream.toByteArray();

                    final ParseFile fileimagem = new ParseFile("imagem.jpg", bytearray);
                    fileimagem.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {

                            if (e == null) {
                                salvarDica(fileimagem);


                            }
                        }
                    });
                } else {
                    salvarDica(null);
                }

And I also have these methods:

private void tirarfoto() {
    Intent abrircamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (abrircamera.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(abrircamera, TIRARFOTO);
    }
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ESCOLHERFOTO && resultCode == RESULT_OK) {

        uricaminhodafoto = data.getData();
        try {
            foto = MediaStore.Images.Media.getBitmap(getContentResolver(), uricaminhodafoto);
            foto = Bitmap.createScaledBitmap(foto, 200, 200, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        picture.setImageBitmap(foto);


    }


    if (requestCode == TIRARFOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        foto = (Bitmap) extras.get("data");
        picture.setImageBitmap(foto);


    }
}

Part of my setup:

 <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/picture"
            android:src="@drawable/picture"
            android:layout_gravity="center_horizontal"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />

The resolution of the saved image is terrible. Does anyone know how to solve it? Thank you.

  • In addition to Victor’s answer, if you want you can read this very explanatory post about image resizing (watch out for memory): http://answall.com/questions/82305/qual%C3%A9-a-more-efficient-way-to-resize-bitmaps-no-android

2 answers

2

Try to replace that:

foto = Bitmap.createScaledBitmap(foto, 200, 200, false);

That’s why:

foto = Bitmap.createScaledBitmap(foto, 1000, 1000, false);

Alternatively, just remove this line and fail to resize Bitmap.

  • But even when it’s a photo and it doesn’t go through this reduction is horrible

  • @Elainebredaschwaner Well, then I don’t know. If I can find the solution I will post another answer.

  • Hello is working well to save images coming from Gallery. Thank you! The camera was not using, since it did not work.

  • I wonder if there is a way to save with a KB number set.

  • @Elainebredaschwaner It is difficult to know the size that the image will occupy before generating it, since image compression algorithms have features that can compress some images better than others, varying according to the colors and textures present in the image. However, strictly speaking you could either reduce the image size (which are these two parameters) or reduce the number corresponding to the quality in the foto.compress(Bitmap.CompressFormat.JPEG, 100, stream);.

0

Well, people, thank you very much to those who collaborated. I gathered them all and found a good solution for me. I couldn’t get the resolution of the photo taken from the camera to look good anyway, so I just let it come from the gallery. But for my App, enough is enough. Now what makes the photo decrease quite the size (stay in the exact size of my Imageview) without losing the definition and be very light to be loaded and for the user to save in the bank was this:

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ESCOLHERFOTO && resultCode == RESULT_OK) {

        uricaminhodafoto = data.getData();
        try {
            foto = MediaStore.Images.Media.getBitmap(getContentResolver(), uricaminhodafoto);


            foto2 = getResizedBitmap(foto);
        } catch (IOException e) {
            e.printStackTrace();
        }

        picture.setImageBitmap(foto2);


    }



}

public Bitmap getResizedBitmap(Bitmap foto) {
    int width = foto.getWidth();
    int height = foto.getHeight();
    if (height > width) {


        newHeight = 400;
        newWidth = (width * 400) / height;
    }
    if (height == width) {

        newHeight = 240;
        newWidth = 240;
    }


    if (width > height) {

        newWidth = 760;
        newHeight = (height * 760 / width);
    }

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
            foto, 0, 0, width, height, matrix, false);
    foto.recycle();

    return resizedBitmap;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.