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

/**
 * Encode an image form the given path into a {@link Base64}  string
 *
 * @param imagePath Path of the image to encode (must be absolute)
 * @return a {@link Base64} encoded string.
 *///  w  ww  . jav  a2 s. c om
public static String encodeBitmap(String imagePath) {
    Bitmap image = BitmapFactory.decodeFile(imagePath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] b = bos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

From source file:Main.java

public static String readAsString(InputStream source, String encoding) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transfer(source, baos);//from  ww w  . ja v a  2  s.  com
    byte[] bytes = baos.toByteArray();
    return new String(bytes, encoding);
}

From source file:Main.java

/**
 * Compress the bitmap to a PNG and return its {@link ByteArrayInputStream}
 * /*  w ww  . j a v  a2  s .c o  m*/
 * @param bitmap The {@link Bitmap} to compress
 * @return The {@link ByteArrayInputStream}
 */
public static ByteArrayInputStream toPNGInputStream(Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    return new ByteArrayInputStream(bytes.toByteArray());
}

From source file:Main.java

public static InputStream Bitmap2InputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;/*ww  w  . j  av a  2s . c  o m*/
}

From source file:Main.java

public static long getBitmapSize(Bitmap bitmap) {
    long length = 0;
    if (bitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
        length = imageInByte.length;//  w  w w .  j  ava 2s.c o  m
    }
    return length;
}

From source file:Main.java

public static InputStream Bitmap2IS(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
    return sbs;/*from  w ww.j ava 2  s .  co m*/
}

From source file:com.woonoz.proxy.servlet.BufferOnCreateInputStream.java

public static BufferOnCreateInputStream create(final InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, baos);/*from w w  w  .ja v a2s  .  c o m*/
    return new BufferOnCreateInputStream(baos.toByteArray());
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {
    if (bitmap == null)
        throw new NullPointerException("bitmap ist null!");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, bos);
    return bos.toByteArray();
}

From source file:Main.java

/**
 * Converts Bitmap into Byte array using PNG compress format.
 * @param bitmap Bitmap//  ww  w.j ava 2s.c om
 * @return byte[]
 */
public static byte[] convertBitmapToByte(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
        return stream.toByteArray();
    }
    return null;
}

From source file:Main.java

public static boolean writeBitmap(Bitmap b) {
    String fn = "logo_qrcode.png";
    ByteArrayOutputStream by = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG, 100, by);
    byte[] stream = by.toByteArray();
    return writeToSdcard(stream, FileSavePath, fn);
}