2

を使用して適切なサイズでデコードした後、ビットマップ(以前にファイルから読み取ったもの)を保存しようとしていますBitmapFactory.Options.inSampleSize。問題は、保存されたビットマップのファイル サイズが元のファイルのサイズの少なくとも 2 倍になることです。私は多くのことを検索しましたが、これに対処する方法を見つけることができませんでした。これは、メモリ効率のために発生させたくないためです (後で保存されたビットマップを再利用します)。これが私が説明することを行う私の方法です:

private Bitmap decodeFileToPreferredSize(File f) {
    Bitmap b = null;
    try {
        // Decode image size
        Log.i("Bitmap", "Imported image size: " + f.length() + " bytes");
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(f.getAbsolutePath(), o);

        //Check if the user has defined custom image size
        int scale = 1;
        if(pref_height != -1 && pref_width != -1) {

            if (o.outHeight > pref_height || o.outWidth > pref_width) {

                scale = (int) Math.pow(
                        2,
                        (int) Math.round(Math.log(pref_width
                                / (double) Math.max(o.outHeight, o.outWidth))
                                / Math.log(0.5)));
            }
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        b = BitmapFactory.decodeFile(f.getAbsolutePath(), o2);
        String name = "Image_" + System.currentTimeMillis() + ".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), name);
        FileOutputStream out = new FileOutputStream(file);
        b.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.close();
        Log.i("Bitmap", "Exported image size: " + file.length() + " bytes");

    } catch (Exception e) {
        b = null;
    }

    return b;
}

更新2 つの画像ファイルの密度を見ました。カメラの意図から来たものは、幅と高さの密度が 72 dpi です。上記の方法で作成された画像ファイルは、幅と高さの密度が 96 dpi です。これは、再スケール係数が (96/72) * 2 ~= 2.5 であるため、カメラから取得した 0.5 MByte の画像が上記の方法で約 2.5 MByte にサイズ変更される理由を説明しています。何らかの理由で、私が作成したビットマップは、カメラからの画像の密度を取りません。BitmapFactory.Options.inDensity のすべてのバリエーションで密度を設定しようとしましたが、うまくいきませんでした。また、ビットマップ密度を変更しようとしましbitmap.setDensity(int dpi);たが、まだ効果がありません。したがって、私の新しい質問は、画像が保存されるときにビットマップの密度を定義する方法があるかどうかです。

前もって感謝します。

4

3 に答える 3

2

同様の問題がありました。Web から画像をダウンロードすると、ブラウザから PC にダウンロードしたときよりも SD の容量が多くなりました。問題は、 BitmapFactory が画像を何らかの最適化されていない形式で保存することだけだと思います。

私の回避策は、bitmapfactory の代わりに以下を使用することでした。

    try {
        try {
            is = yourinputstream;
            // Consider reading stream twice and return the bitmap.

            os = youroutputstream;

            byte data[] = new byte[4096];
            int count;
            while ((count = is.read(data)) != -1) {
                os.write(data, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                is.close();
            }
            if(os != null) {
                os.close();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
于 2012-07-12T10:35:54.407 に答える
2

問題が密度の変化にあることは正しいです。

このスレッドは解決策を明らかにしました: BitmapFactory はソースよりも大きな画像を返します

デコードする前にも無効にしinScaledます:

options.inSampleSize = 2; //or however you desire, power of 2
options.inScaled = false;

これにより、密度が変化しなくなり、探していたサイズの減少が見られます。

于 2016-12-21T00:44:57.310 に答える
0

の場合BitmapFactory.Options.inSampleSize = 0.5、100% / 0.5 = 200% となります。
あなたが望むのはBitmapFactory.Options.inSampleSize = 2、それは 100% / 2 = 50%です

于 2012-07-12T08:47:52.580 に答える