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

/**
 * Convert an image to a byte array/* ww  w . ja  va  2  s .  c  o m*/
 * @param uri
 * @param context
 * @return
 */
public static byte[] convertImageToByte(Uri uri, Context context) {
    byte[] data = null;
    try {
        ContentResolver cr = context.getContentResolver();
        InputStream inputStream = cr.openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        data = baos.toByteArray();
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e2) {
            }
        }
        if (baos != null) {
            try {
                baos.close();
            } catch (Exception e2) {
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:Main.java

public static void saveBitmap(File output, Bitmap bitmap) throws FileNotFoundException, IOException {
    FileOutputStream fos = new FileOutputStream(output);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.flush();/*from  ww w  .java  2  s  .c o  m*/
    fos.close();
    bitmap.recycle();
    bitmap = null;
}

From source file:Main.java

public static void saveImageToPath(Bitmap bitmap, String imagepath) {
    File file = new File(imagepath);
    try {/*from  www . ja  v a  2  s . c  o m*/
        if (!file.exists()) {
            file.createNewFile();
        }
        bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));// quality = 100
    } catch (Exception e) {
    }
}

From source file:Main.java

/**
 * Encodes an ImageView into a base-64 byte array representation. Encoding compression depends
 * on the quality parameter passed. 0 is the most compressed (lowest quality) and 100 is the
 * least compressed (highest quality)./* w w w. j  a va  2  s .  com*/
 *
 * @param context
 * @param res     the resource id of an ImageView
 * @param quality The amount of compression, where 0 is the lowest quality and 100 is the
 *                highest
 * @return the raw base-64 image data compressed accordingly
 */
public static byte[] encodeBase64Image(Context context, int res, int quality) {
    // ensure quality is between 0 and 100 (inclusive)
    quality = Math.max(LOWEST_QUALITY, Math.min(HIGHEST_QUALITY, quality));

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, quality, stream);
    return stream.toByteArray();
}

From source file:Main.java

@NonNull
public static byte[] bitmapToByteArray(@NonNull Bitmap bitmap) {
    ByteArrayOutputStream out = null;
    try {//ww  w  . java  2 s .c  o  m
        out = new ByteArrayOutputStream(bitmap.getWidth() * bitmap.getHeight());
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        return out.toByteArray();
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (Exception ignore) {
            }
    }
}

From source file:Main.java

public static byte[] getImageByte(Bitmap bitmap) {
    // imageView.setDrawingCacheEnabled(true);
    // Bitmap bitMap = imageView.getDrawingCache();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    return imageBytes;
}

From source file:Main.java

public static void createCacheFileFromStream(Bitmap bitmap, String path) {
    File file = new File(path);
    OutputStream out = null;//from w w  w.ja v  a2s .  c  o m
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

private static void blockingStoreBitmap(Context context, Bitmap bitmap, String filename) {
    FileOutputStream fOut = null;
    try {/*from  w  ww. j av a 2s.co  m*/
        fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fOut != null) {
                fOut.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * convert bitmap into byte[]//from   www  .  j  av  a2  s .c  o m
 * @param bitmap the source bitmap
 * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
 *                 small size, 100 meaning compress for max quality. Some
 *                 formats, like PNG which is lossless, will ignore the
 *                 quality setting
 * @return the byte[]
 */
public static byte[] bitmap2bytes(Bitmap bitmap, int quality) {
    if (null == bitmap)
        return new byte[] {};
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    byte[] result = baos.toByteArray();
    try {
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] bitampToByteArray(Bitmap bitmap) {
    byte[] array = null;
    try {//from  w  ww.  j  ava 2s  .co m
        if (null != bitmap) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            array = os.toByteArray();
            os.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return array;
}