Attempting to scale bitmap on Android does not work

I am trying to scale a bitmap. In short, the image comes from ByteArray with a width of 4016. After I scale the image down with the factory options, it still reports a 4016 wide image.

Here are two clips of my code:

Bitmap myBitmap = null; @Override protected byte[] doInBackground(Object... params) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; if (options.outHeight > options.outWidth) { options.inSampleSize = calculateInSampleSize(options, 640, 960); } else { options.inSampleSize = calculateInSampleSize(options, 960, 640); } options.inJustDecodeBounds = false; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); //This log statement outputs 4016!!! Shouldn't it be smaller since I just decoded the byteArray with options? Log.d("bitmap", myBitmap.getWidth()+""); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to // the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } 

Update:

Here are two clips of my code:

 Bitmap myBitmap = null; @Override protected byte[] doInBackground(Object... params) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); if (options.outHeight > options.outWidth) { options.inSampleSize = calculateInSampleSize(options, 640, 960); } else { options.inSampleSize = calculateInSampleSize(options, 960, 640); } options.inJustDecodeBounds = false; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); //This log statement outputs around 1000 now. Log.d("bitmap", myBitmap.getWidth()+""); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to // the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } 
+7
java android bitmap android-bitmap
source share
2 answers

You need to call .decodeByteArray(..) twice! Once to achieve the width and height with .inJustDecodeBounds set to true and then again using .inSampleSize to get the actual scaled bitmap, the options.outHeight and options.outWidth in your code are probably zero.

call BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); before checking outHeight and outWidth .

Edit

Take a look at this example from the Google Android Dev website :

 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; 

Note that options.outHeight used after calling decodeResources(...) . You must fix this in your code.

+3
source share

you did not populate bitmapOptions with setting it to "outHeight" and "outWidth".

you should read google bitmap tutorials:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

in order to decide what the zoom value is, you need to know the image size compared to the destination size. what you need to do decoding twice.

btw, there is another way to reduce the number of images, as I showed here .

0
source share

All Articles