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

/**
 * bitmap -> bytes//from   w w  w  .  j a  v a  2s  . c o  m
 * 
 * */
public static byte[] bitmap2Bytes(Bitmap bitmap) {
    if (null == bitmap) {
        return null;
    }

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

public static byte[] encodeToBytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//  ww w.  jav a2 s  .  c  om

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

static public byte[] getObjectBytes(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);// w w  w  .j  a  v a  2  s  .c om
    oos.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] decodeBitmapToBytes(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (bitmap != null) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    } else {//from ww w  .  j  a v a2 s .c o m
        Log.e(TAG, "bitmap is null");
        return null;
    }
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (bm != null) {

        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

    }//from ww  w  .  j  av  a  2  s  .  c  om

    return baos.toByteArray();
}

From source file:Main.java

/**
 * convert bitmap into byte[]//from   w w  w.  java2  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[] compressBmpToBytes(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);

    return baos.toByteArray();
}

From source file:Main.java

public static String toBase64(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

From source file:Main.java

/**
 * Serialize an object into bytes./* w  ww  . j a  va  2 s .c o m*/
 * @param o Object to be serialized.
 * @return Serialized stream of bytes.
 * @throws IOException
 */
public static byte[] objectToBytes(Serializable o) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(out);
    oo.writeObject(o);
    oo.close();
    return out.toByteArray();
}

From source file:javafx1.Testpics.java

public static String encodeToString(BufferedImage image, String type) {
    String imageString = null;/*from   ww w . j  a v  a  2s .  c om*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();

        BASE64Encoder encoder = new BASE64Encoder();
        imageString = encoder.encode(imageBytes);

        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}