Android Bitmap to Byte Array Convert bitmap2Bytes(Bitmap bm)

Here you can find the source of bitmap2Bytes(Bitmap bm)

Description

bitmap Bytes

License

Apache License

Parameter

Parameter Description
bm a parameter

Declaration

public static byte[] bitmap2Bytes(Bitmap bm) 

Method Source Code

//package com.java2s;
/**//from ww w  . ja  v a 2  s. c  om
 * 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 bm
     * @return
     */
    public static byte[] bitmap2Bytes(Bitmap bm) {
        if (bm == null || bm.isRecycled()) {
            return null;
        }
        byte[] bytes;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        bytes = baos.toByteArray();
        try {
            baos.flush();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    /**
     * @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. convertBitmapToByteArray(Bitmap bitmap)
  2. bitmap2byte(Bitmap bitmap)
  3. putImagePath(String imagePath)
  4. getByteArrayFromImageUri(Activity ctx, Uri uri)
  5. bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat, final boolean needRecycle)
  6. bmpToByteArray(final Bitmap bmp, final boolean needRecycle)
  7. convertBitmapToByte(Bitmap shopbitmap)
  8. getBytes(Bitmap bm)
  9. getByteCount(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat)