Android Bitmap Compress compress(String srcPath, String dstPath, int maxWidth, int maxHeight, long maxSize, CompressFormat format)

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

Description

compress

License

Apache License

Parameter

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

Declaration

public static void compress(String srcPath, String dstPath,
        int maxWidth, int maxHeight, long maxSize, CompressFormat format) 

Method Source Code

//package com.java2s;
/**// w  w  w.jav  a  2 s. co  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. smallPic(String oldPath, String newPath, int size)
  2. compressWithWidth(Bitmap bitmap, int width)
  3. compressWithWidth(String filePath, int width)
  4. compressBitmap(Bitmap bitmap)
  5. compressToBytes(Bitmap bitmap, int quality)
  6. compress(byte[] data, String dstPath, int maxWidth, int maxHeight, long maxSize, CompressFormat format)
  7. decodeToCompressedByteArray(String imageUri)
  8. downsampleBitmap(FileDescriptor fd, int maxArea)