Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class Main {
    /**
     * Bitmap into compressed PNG as InputStream object
     *
     * @param image the Bitmap
     * @return compressed PNG as InputStream object
     */
    public static ByteArrayInputStream bitmapToPngInputStream(final Bitmap image) {
        return new ByteArrayInputStream(bitmapToPng(image));
    }

    /**
     * Bitmap into compressed PNG
     *
     * @param image the Bitmap
     * @return byte array of PNG
     */
    public static byte[] bitmapToPng(final Bitmap image) {
        if (image == null)
            return null;

        ByteArrayOutputStream ba = new ByteArrayOutputStream();

        if (image.compress(CompressFormat.PNG, 100, ba))
            return ba.toByteArray();
        else
            return null;
    }
}