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;
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; }
java android bitmap android-bitmap
user2676468
source share