Android: resizing bitmaps without losing quality

I did browse through the entire network prior to publication. My problem is that I cannot resize a bitmap image without losing image quality (the quality is really poor and uneven).

I accept a bitmap from the camera, and then I need to scale it, so I can upload it to the server much faster.

This is the function that performs the selection.

public Bitmap resizeBitmap(Bitmap bitmap){ Canvas canvas = new Canvas(); Bitmap resizedBitmap = null; if (bitmap !=null) { int h = bitmap.getHeight(); int w = bitmap.getWidth(); int newWidth=0; int newHeight=0; if(h>w){ newWidth = 600; newHeight = 800; } if(w>h){ newWidth = 800; newHeight = 600; } float scaleWidth = ((float) newWidth) / w; float scaleHeight = ((float) newHeight) / h; Matrix matrix = new Matrix(); // resize the bit map matrix.preScale(scaleWidth, scaleHeight); resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawBitmap(resizedBitmap, matrix, paint); } return resizedBitmap; 

and this is how I get the image from the activity result

  protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode != RESULT_CANCELED){ if (requestCode == 0) { if (resultCode == RESULT_OK) { getContentResolver().notifyChange(mImageUri, null); ContentResolver cr = this.getContentResolver(); try { b = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri); Log.d("foto", Integer.toString(b.getWidth())); Log.d("foto", Integer.toString(b.getHeight())); addPhoto.setImageBitmap(b); } catch (Exception e) { Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); Log.d("TAG", "Failed to load", e); } } } 

I'm starting to think that the best way to get a small image size is to set the camera resolution. Can anybody help?

+4
source share
3 answers

Try Bitmap.createScaledBitmap . It also has the ability to filter the source.

+1
source

A good scaling algorithm (not the nearest neighbor) consists of just two steps (plus calculating the exact Rect value for the I / O image lesson):

  • using BitmapFactory.Options :: inSampleSize-> BitmapFactory.decodeResource () as close as possible to the required resolution, but not less
  • get exact resolution by zooming out a bit using Canvas :: drawBitmap ()

Here is a detailed explanation of how SonyMobile solved this problem: http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

Here is the source code for the SonyMobile scaling utility: http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/

+6
source

Try the code below to resize the bitmap.

  public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) { int width = bmp.getWidth(); int height = bmp.getHeight(); 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 newBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false); return newBitmap ; } 

I used this code to reduce the size of the bitmap, and its quality, well ..., was acceptable.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1479941/


All Articles