2016-12-29 19 views
1

を圧縮した後、私はBitmapを圧縮するために使用されるコードの一部は次のとおりです。ビットマップサイズが大きくなる。ここ

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
System.out.println("before: " + bmp.getByteCount()); 
bmp.compress(Bitmap.CompressFormat.JPEG, 80, baos); 
System.out.println("baos: " + baos.toByteArray().length); 
BitmapFactory.Options options = new BitmapFactory.Options(); 
Bitmap b = BitmapFactory.decodeStream(new ByteArrayInputStream(baos.toByteArray()), 
      null, options); 
System.out.println("after: " + b.getByteCount()); 

LogCat出力:

12-29 11:45:52.638 18042-18042/com.xxx.yyy I/System.out: before: 653760 
12-29 11:45:52.678 18042-18042/com.xxx.yyy I/System.out: baos: 13118 
12-29 11:45:52.688 18042-18042/com.xxx.yyy I/System.out: after: 1307520 

baosの大きさは、圧縮後のBitmapの大きさは、ようだが、なぜb.getByteCount()は、圧縮する前にbmpより大きいサイズを返しますか?画像Captured.iは、あなたがしようと、それをhere.Using私のコードを入れた後

+1

'quaiity'パラメータの値を小さくして圧縮してみます。 –

答えて

0
BitmapFactory.Options options = new BitmapFactory.Options(); 

/* 
    isSampleSize will reduce your bitmap size. 
    If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save 
    memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the 
    decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original 
    and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based 
    on powers of 2, any other value will be rounded down to the nearest power of 2. 
*/ 
options.inSampleSize = 2; 
Bitmap b = BitmapFactory.decodeStream(new ByteArrayInputStream(baos.toByteArray()), null, options); 
System.out.println("after: " + b.getByteCount()); 
0

私は同じ問題を抱えていたが、私はいくつかのMethod.iを使用して解決は私のカメラアプリケーションでこの方法を使用していました。 私の方法:

public static Bitmap decodeSampledBitmapFromByte(Context context, byte[] bitmapBytes) { 
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 

    int reqWidth, reqHeight; 
    Point point = new Point(); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
     display.getSize(point); 
     reqWidth = point.x; 
     reqHeight = point.y; 
    } else { 
     reqWidth = display.getWidth(); 
     reqHeight = display.getHeight(); 
    } 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    options.inMutable = true; 
    options.inBitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Load & resize the image to be 1/inSampleSize dimensions 
    // Use when you do not want to scale the image with a inSampleSize that is a power of 2 
    options.inScaled = true; 
    options.inDensity = options.outWidth; 
    options.inTargetDensity = reqWidth * options.inSampleSize; 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; // If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels. 
    options.inPurgeable = true;   // Tell to gc that whether it needs free memory, the Bitmap can be cleared 
    options.inInputShareable = true; // Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future 

    return BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options); 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    int initialInSampleSize = computeInitialSampleSize(options, reqWidth, reqHeight); 

    int roundedInSampleSize; 
    if (initialInSampleSize <= 8) { 
     roundedInSampleSize = 1; 
     while (roundedInSampleSize < initialInSampleSize) { 
      // Shift one bit to left 
      roundedInSampleSize <<= 1; 
     } 
    } else { 
     roundedInSampleSize = (initialInSampleSize + 7)/8 * 8; 
    } 

    return roundedInSampleSize; 
} 

private static int computeInitialSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final double height = options.outHeight; 
    final double width = options.outWidth; 

    final long maxNumOfPixels = reqWidth * reqHeight; 
    final int minSideLength = Math.min(reqHeight, reqWidth); 

    int lowerBound = (maxNumOfPixels < 0) ? 1 : 
      (int) Math.ceil(Math.sqrt(width * height/maxNumOfPixels)); 
    int upperBound = (minSideLength < 0) ? 128 : 
      (int) Math.min(Math.floor(width/minSideLength), 
        Math.floor(height/minSideLength)); 

    if (upperBound < lowerBound) { 
     // return the larger one when there is no overlapping zone. 
     return lowerBound; 
    } 

    if (maxNumOfPixels < 0 && minSideLength < 0) { 
     return 1; 
    } else if (minSideLength < 0) { 
     return lowerBound; 
    } else { 
     return upperBound; 
    } 
} 

私はuが私の方法decodeSampledBitmapFromByteを(使用する必要があると思う)method.youは、あなたのコード内のすべてのメソッドを配置する必要があり、あなたの代わりに の。

このコードを使用すると、同じサイズになります。compress.iの後にLogCatを配置します。

My Logcate

は、それはあなたが...お楽しみに役立つことを願っています(:。

関連する問題