Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

public class Main {
    /**
     * convert bitmap into byte[]
     * @param bitmap the source bitmap
     * @return the byte[]
     */
    public static byte[] bitmap2bytes(Bitmap bitmap) {
        return bitmap2bytes(bitmap, 100);
    }

    /**
     * convert bitmap into byte[]
     * @param bitmap the source bitmap
     * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
     *                 small size, 100 meaning compress for max quality. Some
     *                 formats, like PNG which is lossless, will ignore the
     *                 quality setting
     * @return the byte[]
     */
    public static byte[] bitmap2bytes(Bitmap bitmap, int quality) {
        if (null == bitmap)
            return new byte[] {};
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        byte[] result = baos.toByteArray();
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}