Example usage for android.graphics Bitmap compress

List of usage examples for android.graphics Bitmap compress

Introduction

In this page you can find the example usage for android.graphics Bitmap compress.

Prototype

@WorkerThread
public boolean compress(CompressFormat format, int quality, OutputStream stream) 

Source Link

Document

Write a compressed version of the bitmap to the specified outputstream.

Usage

From source file:Main.java

public static boolean rotateBitmap(File inFile, File outFile, int angle)
        throws FileNotFoundException, IOException {
    // Declare//from   w  ww.  j  a v a  2 s  .  c om
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    // Create options
    BitmapFactory.Options options = new BitmapFactory.Options();
    // Create transform matrix
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    // Increment inSampleSize progressively to reduce image resolution and size. If
    // the program is properly managing memory, and you don't have other large images
    // loaded in memory, this loop will generally not need to go through more than 3
    // iterations. To be safe though, we stop looping after a certain amount of tries
    // to avoid infinite loops
    for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
        try {
            // Load the bitmap from file
            inStream = new FileInputStream(inFile);
            Bitmap originalBitmap = BitmapFactory.decodeStream(inStream, null, options);
            // Rotate the bitmap
            Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            // Save the rotated bitmap
            outStream = new FileOutputStream(outFile);
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.close();
            // Recycle the bitmaps to immediately free memory
            originalBitmap.recycle();
            originalBitmap = null;
            rotatedBitmap.recycle();
            rotatedBitmap = null;
            // Return
            return true;
        } catch (OutOfMemoryError e) {
            // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
        } finally {
            // Clean-up if we failed on save
            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                }
            }
        }
    }
    // Failed
    return false;
}

From source file:com.kku.apps.pricesearch.util.Utils.java

public static byte[] getByteImage(Bitmap image) {

    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(CompressFormat.PNG, 100, stream);

    return stream.toByteArray();
}

From source file:Main.java

public static void saveMyBitmap(Bitmap mBitmap) {
    File f = new File(FileSavePath + "qrcode.png");
    try {//from  w  ww  .ja v a 2s .  com
        f.createNewFile();
    } catch (IOException e) {
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, String file, CompressFormat format) {
    OutputStream os = null;//from   w  w  w .j  a  v a 2 s .  c  o  m
    try {
        File f = new File(file);
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        os = new BufferedOutputStream(new FileOutputStream(file));
        if (bitmap.compress(format, 100, os)) {
            os.flush();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
    return false;
}

From source file:Main.java

public static void saveBitmap(Bitmap bm, String picName) {
    try {/*  w  ww .  j  ava2 s . c om*/
        if (!isFileExist("")) {
            File tempf = createSDDir("");
        }
        File f = new File(SDPATH, picName + ".JPEG");
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream out = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void writeBitmap(Bitmap bitmap, String path) throws IOException {
    OutputStream out = null;/*from   w w  w  .  j  a v a 2s  .com*/
    try {
        File file = new File(path);
        if (file.exists() && file.length() > 0)
            return;
        out = new BufferedOutputStream(new FileOutputStream(file), 4096);
        if (bitmap != null)
            bitmap.compress(CompressFormat.JPEG, 90, out);
    } catch (Exception e) {
        Log.e(TAG, "writeBitmap failed : " + e.getMessage());
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:Main.java

public static String saveFile(Bitmap bm, String dirPath, String fileName, int scale) {
    File dirFile = new File(dirPath);
    if (!dirFile.exists()) {
        dirFile.mkdirs();//w  w  w .  jav  a  2  s  .  com
    }
    File myCaptureFile = new File(dirPath + fileName);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, scale, bos);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dirPath + fileName;
}

From source file:Main.java

public static void saveBitmap(Bitmap bm, String picName) {
    try {// ww w  . j a  v  a2 s .c  o m
        if (!isFileExist("")) {
            File tempf = createSDDir("");
        }
        File f = new File(SDPATH, picName + ".JPEG");
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream out = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveBitmap2file(Bitmap bmp, String filename) {
    Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
    int quality = 100;
    OutputStream stream = null;/*  w w w  .j a  va2 s. com*/
    try {
        stream = new FileOutputStream(filename);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bmp.compress(format, quality, stream);
}

From source file:grytsenko.coworkers.web.WebClient.java

public static byte[] toPng(byte[] raw) throws IOException {
    Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
    if (bitmap == null) {
        throw new IOException("Not decoded.");
    }/*from  ww  w  .ja  va2s.c o m*/

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    boolean compressed = bitmap.compress(CompressFormat.PNG, PHOTO_QUALITY, output);
    if (!compressed) {
        throw new IOException("Not compressed.");
    }

    return output.toByteArray();
}