8

ビットマップを縮小しようとしています。要するに、画像はもともと幅が 4016 の ByteArray からのものです。工場出荷時のオプションで画像を縮小した後も、幅が 4016 の画像が報告されます。

ここに私のコードの2つのクリップがあります:

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;
        }

アップデート:

ここに私のコードの2つのクリップがあります:

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;
        }
4

2 に答える 2

0

「outHeight」と「outWidth」を設定するものを bitmapOptions に入力しませんでした。

Google のビットマップ チュートリアルをチェックしてください。

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

スケーリングする値を決定するには、目的のサイズと比較した画像のサイズを知る必要があります。そのため、デコードを 2 回行う必要があります。

ところで、ここで示したように、画像をダウンサンプリングする別の方法があります。

于 2014-01-10T10:22:13.520 に答える