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.Canvas;
import android.graphics.Color;

import android.graphics.Rect;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * save the bitmap to local,add give a white bg color
     * @param bitmap
     * @param path
     * @return
     */
    public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) {
        BufferedOutputStream bos = null;
        try {
            File file = new File(path);
            if (file.exists())
                file.delete();
            bos = new BufferedOutputStream(new FileOutputStream(file));

            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
            int w_new = w;
            int h_new = h;
            Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888);
            //            Paint paint = new Paint();
            //            paint.setColor(Color.WHITE);
            Canvas canvas = new Canvas(resultBitmap);
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null);
            resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            resultBitmap.recycle();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}