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 byte[] compressToBytes(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(65536);
    bitmap.compress(CompressFormat.JPEG, quality, baos);
    return baos.toByteArray();
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getBitmapStrBase64(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();
    String data = Base64.encodeToString(bytes, 0, bytes.length, Base64.DEFAULT);

    if (bitmap != null) {
        bitmap.recycle();//from   w ww  . j  ava 2s.co m
        bitmap = null;
    }

    return data;
}

From source file:Main.java

public static String toXml(Object model) throws JAXBException, IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
    marshal(model, output);/*  w  ww . j av  a  2 s. com*/
    output.flush();
    return new String(output.toByteArray(), "UTF-8");
}

From source file:Main.java

public static byte[] path2Bytes(String path) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BitmapFactory.decodeFile(path).compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:fr.free.divde.webcam.image.DataImageUrl.java

public static String imageToDataURL(BufferedImage image, String format, String mimeType) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(image, format, out);
    byte[] imageBytes = out.toByteArray();
    String base64 = Base64.encodeBase64String(imageBytes);
    StringBuilder res = new StringBuilder("data:");
    res.append(mimeType);//  ww w  .  j  av  a  2s  .  c  om
    res.append(";base64,");
    res.append(base64);
    return res.toString();
}

From source file:Main.java

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

}

From source file:Main.java

public static void writeBitmapToFile(Bitmap bitmap, File file) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    byte[] bitmapData = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bitmapData);/*from w ww  . j ava2 s . c  o m*/
    fos.flush();
    fos.close();
}

From source file:Main.java

/**
 * Encodes given <tt>Bitmap</tt> to array of bytes using given
 * compression <tt>quality</tt> in PNG format.
 * @param bmp the bitmap to encode.// w  ww.ja va 2 s  .  c  om
 * @param quality encoding quality in range 0-100.
 * @return raw bitmap data PNG encoded using given <tt>quality</tt>.
 */
public static byte[] convertToBytes(Bitmap bmp, int quality) {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, quality, bout);
    return bout.toByteArray();
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from   www. j  a  v  a2 s  . c o m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] concatByteArrays(byte[] a, byte[] b) {
    try {/*from   ww  w .j av a 2  s. c  om*/
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(a);
        outputStream.write(b);
        return outputStream.toByteArray();
    } catch (Exception e) {
        return null;
    }
}