Android Bitmap Compress compress(byte[] data, String dstPath, int maxWidth, int maxHeight, long maxSize, CompressFormat format)

Here you can find the source of compress(byte[] data, String dstPath, int maxWidth, int maxHeight, long maxSize, CompressFormat format)

Description

compress

License

Apache License

Parameter

Parameter Description
data a parameter
dstPath a parameter
maxWidth a parameter
maxHeight a parameter
maxSize a parameter
format a parameter

Declaration

public static void compress(byte[] data, String dstPath, int maxWidth,
        int maxHeight, long maxSize, CompressFormat format) 

Method Source Code

//package com.java2s;
/**// www  . j a  va  2  s.  c o m
 * Copyright (c) 2013-2014, Rinc Liu (http://rincliu.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import android.graphics.BitmapFactory;

public class Main {
    /**
     * @param srcPath
     * @param dstPath
     * @param maxWidth
     * @param maxHeight
     * @param maxSize
     * @param format
     */
    public static void compress(String srcPath, String dstPath,
            int maxWidth, int maxHeight, long maxSize, CompressFormat format) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, opts);
        opts.inJustDecodeBounds = false;
        int w = opts.outWidth;
        int h = opts.outHeight;
        int size = 0;
        if (w <= maxWidth && h <= maxHeight) {
            size = 1;
        } else {
            // The decoder uses a final value based on powers of 2,
            // any other value will be rounded down to the nearest power of 2.
            // So we use a ceil log value to keep both of them under limits.
            // See doc:
            // http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize
            double scale = w >= h ? w / maxWidth : h / maxHeight;
            double log = Math.log(scale) / Math.log(2);
            double logCeil = Math.ceil(log);
            size = (int) Math.pow(2, logCeil);
        }
        opts.inSampleSize = size;
        bitmap = BitmapFactory.decodeFile(srcPath, opts);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int quality = 100;
        bitmap.compress(format, quality, baos);
        while (baos.toByteArray().length > maxSize) {
            baos.reset();
            bitmap.compress(format, quality, baos);
            quality -= 10;
        }
        try {
            baos.writeTo(new FileOutputStream(dstPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.flush();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param data
     * @param dstPath
     * @param maxWidth
     * @param maxHeight
     * @param maxSize
     * @param format
     */
    public static void compress(byte[] data, String dstPath, int maxWidth,
            int maxHeight, long maxSize, CompressFormat format) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
                opts);
        opts.inJustDecodeBounds = false;
        int w = opts.outWidth;
        int h = opts.outHeight;
        int size = 0;
        if (w <= maxWidth && h <= maxHeight) {
            size = 1;
        } else {
            // The decoder uses a final value based on powers of 2,
            // any other value will be rounded down to the nearest power of 2.
            // So we use a ceil log value to keep both of them under limits.
            // See doc:
            // http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize
            double scale = w >= h ? w / maxWidth : h / maxHeight;
            double log = Math.log(scale) / Math.log(2);
            double logCeil = Math.ceil(log);
            size = (int) Math.pow(2, logCeil);
        }
        opts.inSampleSize = size;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int quality = 100;
        bitmap.compress(format, quality, baos);
        while (baos.toByteArray().length > maxSize) {
            baos.reset();
            bitmap.compress(format, quality, baos);
            quality -= 10;
        }
        try {
            baos.writeTo(new FileOutputStream(dstPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.flush();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. compressWithWidth(Bitmap bitmap, int width)
  2. compressWithWidth(String filePath, int width)
  3. compressBitmap(Bitmap bitmap)
  4. compressToBytes(Bitmap bitmap, int quality)
  5. compress(String srcPath, String dstPath, int maxWidth, int maxHeight, long maxSize, CompressFormat format)
  6. decodeToCompressedByteArray(String imageUri)
  7. downsampleBitmap(FileDescriptor fd, int maxArea)