Example usage for java.io ByteArrayOutputStream toByteArray

List of usage examples for java.io ByteArrayOutputStream toByteArray

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream toByteArray.

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Creates a newly allocated byte array.

Usage

From source file:Main.java

public static String getBase64FromBitmap(Bitmap bitmap, int bitmapQuality) {
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, bitmapQuality, bStream);
    byte[] bytes = bStream.toByteArray();
    return Base64.encodeToString(bytes, Base64.DEFAULT);
}

From source file:Main.java

public static String bitmapToBase64String(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
    byte[] bitmapArray = baos.toByteArray();

    String encodeString = Base64.encodeToString(bitmapArray, Base64.DEFAULT);

    return encodeString;
}

From source file:Main.java

public static ByteArrayInputStream Bitmap2ByteArrayInputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    return new ByteArrayInputStream(baos.toByteArray());
}

From source file:Main.java

public static String bitmapToString(Bitmap bitmap) {
    String bitmapString = null;/* w ww  . j a va  2s . com*/
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
    byte[] bytes = bStream.toByteArray();
    bitmapString = Base64.encodeToString(bytes, Base64.DEFAULT);
    return bitmapString;
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap $bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    $bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    return byteArray;
}

From source file:Main.java

/***
 * /*from w ww.  j  ava2  s .  co  m*/
 * @param imageId The image from layout
 * @param resources From which activity
 * @return Serialized image
 */
public static byte[] ImageToByte(int imageId, Resources resources) {
    Bitmap bitmap = BitmapFactory.decodeResource(resources, imageId);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static String DocumentToString(Document doc) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ElementToStream(doc.getDocumentElement(), baos);
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * Bitmap into compressed jpeg//from w  ww. ja  v  a2s.c om
 *
 * @param image the Bitmap
 * @param quality quality of compression
 * @return byte array of jpeg
 */
public static byte[] bitmapToJpg(final Bitmap image, final int quality) {
    if (image == null)
        return null;

    ByteArrayOutputStream ba = new ByteArrayOutputStream();

    if (image.compress(CompressFormat.JPEG, quality, ba))
        return ba.toByteArray();
    else
        return null;
}

From source file:Main.java

static byte[] serialize(Serializable obj) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(obj);/*from  w  ww .  j ava  2s  .  co  m*/
    return b.toByteArray();
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();
    try {/* w  ww .  j  a v a  2  s . c o  m*/
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytes;

}