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 void saveJpegFile(String fileName, Bitmap bitmap) {
    // file/*from w w w  .  j  av a2  s .c  om*/
    File file = new File(extStorageDirectory, fileName + ".jpg");
    File fileDirectory = new File(extStorageDirectory);
    if (!fileDirectory.exists()) {
        fileDirectory.mkdir();
    }
    OutputStream outStream = null;
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean OutPutImage(File file, Bitmap bitmap) {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*w  w w . j  a v a  2  s.c om*/
    }
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        if (bos != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            fos.flush();
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        return false;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 * This is only used when the launcher shortcut is created.
 * /*from   ww w .j a v  a 2  s  .c  om*/
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *            be cropped.
 * @param size The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 *         shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    Bitmap blurbitmap = null;

    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inJustDecodeBounds = true;
        blurbitmap = BitmapFactory.decodeStream(bs, null, options);
        options.inJustDecodeBounds = false;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bs != null) {
            try {
                bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * blurbitmap.getWidth());
    final int mHeight = Math.round(mScale * blurbitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;

}

From source file:Main.java

public static void saveBackgroundImage(Context ctx, String filePath, Bitmap bitmap, int quality)
        throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();//from  w ww  .j a v a2  s  . c o m
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.PNG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:com.freshplanet.ane.GoogleCloudStorageUpload.tasks.UploadToGoogleCloudStorageAsyncTask.java

private static byte[] getJPEGRepresentationFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    return outputStream.toByteArray();
}

From source file:Main.java

private static void saveSmallerImage(String pathOfInputImage, int dstWidth, int dstHeight) {
    try {/*from ww  w  .  j av  a 2s  .  c  o m*/
        int inWidth = 0;
        int inHeight = 0;

        InputStream in = new FileInputStream(pathOfInputImage);

        // decode image size (decode metadata only, not the whole image)
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        in = null;

        // save width and height
        inWidth = options.outWidth;
        inHeight = options.outHeight;

        // decode full image pre-resized
        in = new FileInputStream(pathOfInputImage);
        options = new BitmapFactory.Options();
        // calc rought re-size (this is no exact resize)
        options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight);
        // decode full image
        Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

        // calc exact destination size
        Matrix m = new Matrix();
        RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
        RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
        m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
        float[] values = new float[9];
        m.getValues(values);

        // resize bitmap
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
                (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);

        // save image
        try {
            FileOutputStream out = new FileOutputStream(pathOfInputImage);
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        } catch (Exception e) {
            Log.e("Image", e.getMessage(), e);
        }
    } catch (IOException e) {
        Log.e("Image", e.getMessage(), e);
    } catch (Exception e) {
        Log.e("Image", e.getMessage());
    }
}

From source file:com.platform.middlewares.plugins.CameraPlugin.java

private static String writeToFile(Context context, Bitmap img) {
    String name = null;//from   w w  w  .j  ava2 s. c  om
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        img.compress(Bitmap.CompressFormat.JPEG, 50, out);
        name = CryptoHelper.base58ofSha256(out.toByteArray());
        File storageDir = new File(context.getFilesDir().getAbsolutePath() + "/pictures/");
        File image = new File(storageDir, name + ".jpeg");
        FileUtils.writeByteArrayToFile(image, out.toByteArray());
        return name;
        // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static void save(String path, Bitmap bitmap) {

    String name = path.substring(path.lastIndexOf("/"));
    File file = new File(SAVE_PATH + name);
    try {// w  w w.j a  v  a 2  s  .com
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap != null) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        }
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static File savebitmap(Bitmap bitmap) {
    OutputStream outStream = null;
    File folder = new File(Environment.getExternalStorageDirectory().toString() + "/InSyte");
    folder.mkdirs();/*from  w  w  w.  j  a v a  2 s  . c o  m*/
    String mDirectory = folder.toString();
    File file = new File(mDirectory, "IMG_" + System.currentTimeMillis() + ".jpg");
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file;

}

From source file:Main.java

public static void saveToFile(Bitmap... bitmaps) {
    int i = 0;/*www . j av  a 2 s  .c  om*/
    for (Bitmap bitmap : bitmaps) {
        String path = Environment.getExternalStorageDirectory().getPath();
        File file = new File(path + "/" + (i++) + ".jpg");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}