Android Bitmap Save saveBitmapJPEGWithBackgroundColor( String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor)

Here you can find the source of saveBitmapJPEGWithBackgroundColor( String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor)

Description

Save jpeg image with background color

Parameter

Parameter Description
strFileName Save file path
bitmap Input bitmap
nQuality Jpeg quality for saving
nBackgroundColor background color

Return

whether success or not

Declaration

public static boolean saveBitmapJPEGWithBackgroundColor(
        String strFileName, Bitmap bitmap, int nQuality,
        int nBackgroundColor) 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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

import android.graphics.Canvas;

import android.graphics.Paint;
import android.graphics.Rect;

public class Main {
    /**//from  w w  w.  j  a  v  a2s . c om
     * Save jpeg image with background color
     * @param strFileName Save file path
     * @param bitmap Input bitmap
     * @param nQuality Jpeg quality for saving
     * @param nBackgroundColor background color
     * @return whether success or not
     */
    public static boolean saveBitmapJPEGWithBackgroundColor(
            String strFileName, Bitmap bitmap, int nQuality,
            int nBackgroundColor) {
        boolean bSuccess1 = false;
        boolean bSuccess2 = false;
        boolean bSuccess3;
        File saveFile = new File(strFileName);

        if (saveFile.exists()) {
            if (!saveFile.delete())
                return false;
        }

        int nA = (nBackgroundColor >> 24) & 0xff;

        // If Background color alpha is 0, Background color substitutes as white
        if (nA == 0)
            nBackgroundColor = 0xFFFFFFFF;

        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawColor(nBackgroundColor);
        canvas.drawBitmap(bitmap, rect, rect, new Paint());

        // Quality limitation min/max
        if (nQuality < 10)
            nQuality = 10;
        else if (nQuality > 100)
            nQuality = 100;

        OutputStream out = null;

        try {
            bSuccess1 = saveFile.createNewFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            out = new FileOutputStream(saveFile);
            bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality,
                    out);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (out != null) {
                out.flush();
                out.close();
                bSuccess3 = true;
            } else
                bSuccess3 = false;

        } catch (IOException e) {
            e.printStackTrace();
            bSuccess3 = false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return (bSuccess1 && bSuccess2 && bSuccess3);
    }
}

Related

  1. saveBitmap(Bitmap image)
  2. saveBitmap(File file, Bitmap bitmap)
  3. saveBitmap(File saveToFile, Bitmap bitmapToSave)
  4. saveBitmapAsJpgAtSd(Bitmap mBitmap, String dirPath, String fileName)
  5. saveBitmapFromView(View view, String path)
  6. saveBitmapPNGWithBackgroundColor( String strFileName, Bitmap bitmap, int nBackgroundColor)
  7. saveBitmapToPath(Context ctxt, String path, String filename, Bitmap bm)
  8. saveJPEGBitmap(Bitmap bmp, String path)
  9. saveJpegFile(String fileName, Bitmap bitmap)