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

private static InputStream getDrawableStream(Context context, String rawName) throws IOException {
    int id = context.getResources().getIdentifier(rawName, "drawable", context.getPackageName());
    if (id != 0) {
        BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(id);
        Bitmap bitmap = drawable.getBitmap();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, os);
        return new ByteArrayInputStream(os.toByteArray());
    }/*w  w w .j  av  a 2s . co  m*/

    throw new IOException(String.format("bitmap of id: %s from %s not found", id, rawName));
}

From source file:Main.java

private static byte[] bitmapToBytes(Bitmap in) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    in.compress(CompressFormat.PNG, 0, bos);
    return bos.toByteArray();
}

From source file:Main.java

public static String simpleCompressImage(String path, String newPath) {
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    FileOutputStream outputStream = null;
    try {//from  ww  w. j a  v  a  2 s  .co  m
        outputStream = new FileOutputStream(newPath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    recycle(bitmap);
    return newPath;
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, String filename) {
    boolean status = true;
    FileOutputStream out = null;//from   ww w  .  j av  a2 s.  co m
    try {
        out = new FileOutputStream(filename);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        status = false;
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return status;
    }
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {
    if (bitmap == null)
        throw new NullPointerException("bitmap ist null!");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, bos);
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] toByteArray(Bitmap bitmap) {
    if (bitmap == null)
        return null;

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // Compress image to lower quality scale 1 - 100
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] image = stream.toByteArray();
    return image;
}

From source file:Main.java

public static void saveBitmapToSDCard(Bitmap bitmap, String path) {
    OutputStream outputStream = null;
    try {/*from   w w w .  j ava  2 s.  c o  m*/
        outputStream = new FileOutputStream(path);
        if (outputStream != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
            outputStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveJPGE_After(Context context, Bitmap bitmap, String path) {
    File file = new File(context.getExternalCacheDir() + path);
    try {/*  w  w w .  j av a2  s  . co m*/
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap.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 void saveRgb2Bitmap(IntBuffer buf, String filePath, int width, int height) {
    final int[] pixelMirroredArray = new int[width * height];
    Log.d("TryOpenGL", "Creating " + filePath);
    BufferedOutputStream bos = null;
    try {/*from ww  w. j a  v a 2s  .  com*/
        int[] pixelArray = buf.array();
        // rotate 180 deg with x axis because y is reversed
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
            }
        }
        bos = new BufferedOutputStream(new FileOutputStream(filePath));
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bmp.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray));
        bmp.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        bmp.recycle();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    if (bmp == null || bmp.isRecycled())
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/*from w ww  . j a v  a2s.  c  o m*/
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}