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 ByteArrayInputStream Bitmap2InputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    ByteArrayInputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;/*from   w w  w  .ja v  a  2 s  .  c  o  m*/
}

From source file:Main.java

public static InputStream getBitmapInputStream(Bitmap bitmap) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
    return bs;//from  w  w  w .  ja v a 2  s.  c o  m
}

From source file:Main.java

/**
 * @author Casper Rasmussen - 2012/*from w w w. j  a  va2  s  .  c  o m*/
 * Use this method to get byte[] from a file of a given path.
 * @param filePath
 * @param compressRate - 0-100%, 100% means no compression
 * @param mCompressFormat - CompressFormat.PNG, CompressFormat.JPEG etc.
 * @return byte[]
 */
public static byte[] filePathToByteArray(String filePath, int compressRate, CompressFormat mCompressFormat) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    Bitmap bitmapFromFile = BitmapFactory.decodeFile(filePath, options);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmapFromFile.compress(mCompressFormat, compressRate, stream);
    byte[] byteArray = stream.toByteArray();

    return byteArray;
}

From source file:Main.java

public static InputStream bitmap2InputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;//from w  w w  .j  av  a 2  s.  co m
}

From source file:Main.java

public static byte[] getBytesFromInput(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[in.available()];
    in.read(buffer);/* w  w w .  j a  v a 2 s  .  c om*/
    out.write(buffer);
    return out.toByteArray();
}

From source file:Main.java

public static String bmpToString(Bitmap bitmap) {
    String bmpStr = "";
    try {/*from  w  w w.j a  v a2  s  .co m*/
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        byte[] bts = outputStream.toByteArray();
        bmpStr = Base64.encodeToString(bts, Base64.DEFAULT);
    } catch (Exception e) {
        // TODO: handle exception
    }
    return bmpStr;
}

From source file:Main.java

public static String encodeToBase64(Bitmap image) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] bytes = byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(bytes, Base64.DEFAULT);
}

From source file:Main.java

public static String bitmapToBase64(Bitmap bmp) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 75, byteArrayOutputStream);
    byte[] imgBytes = byteArrayOutputStream.toByteArray();

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

From source file:Main.java

public static String serialize(Bitmap poster) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    poster.compress(FORMAT, QUALITY, byteArrayOutputStream);
    return Base64.encodeToString(byteArrayOutputStream.toByteArray(), BASE64_FLAGS);
}

From source file:Main.java

public static byte[] objectToByte(Object obj) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(outputStream);
    os.writeObject(obj);/*from   www.  j a  va 2s  .co  m*/
    return outputStream.toByteArray();
}