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[] ImageToByte(ImageView image) {
    Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
    Bitmap resized = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    resized.compress(Bitmap.CompressFormat.PNG, 100, baos);

    return baos.toByteArray();
}

From source file:Main.java

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

    return Base64.encodeToString(bytes, Base64.NO_WRAP);
}

From source file:Main.java

private static byte[] getBytesFromDriveImageUri(Context context, Uri uri) {
    InputStream inputStream = null;
    try {/* w ww . ja v a2  s.  c  o m*/
        inputStream = context.getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(inputStream);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

    return stream.toByteArray();
}

From source file:Main.java

public static byte[] convertToByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    byte[] byteArray = stream.toByteArray();
    try {//w  ww  .j a  v  a2s  .  co m
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArray;
}

From source file:Main.java

private static byte[] resizeImage(byte[] input, int length) {
    Bitmap original = BitmapFactory.decodeByteArray(input, 0, input.length);
    Bitmap resized = Bitmap.createScaledBitmap(original, length, length, true);

    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    resized.compress(Bitmap.CompressFormat.JPEG, 100, blob);

    return blob.toByteArray();
}

From source file:Main.java

/**
 * convert Bitmap to byte array//from  ww w  .j a  v  a  2  s. c  o m
 *
 * @param b
 * @return
 */
public static byte[] bitmapToByte(Bitmap b) {
    if (b == null) {
        return null;
    }

    ByteArrayOutputStream o = new ByteArrayOutputStream();
    b.compress(CompressFormat.PNG, 100, o);
    return o.toByteArray();
}

From source file:Main.java

public static byte[] bmpToByteArray(Bitmap bmp) {
    if (bmp == null)
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 80, output);

    byte[] result = output.toByteArray();
    try {/*from  www .  ja  v a 2  s.c  o  m*/
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static void compressWithQuality(Bitmap image, String outPath, int maxSize) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    image.compress(Bitmap.CompressFormat.JPEG, 20, os);

    while (os.toByteArray().length / 1024 > 1024) {
        os.reset();/*from w  ww  .jav a  2 s.c  o  m*/
        image.compress(Bitmap.CompressFormat.JPEG, 20, os);
    }

    ByteArrayInputStream bi = new ByteArrayInputStream(os.toByteArray());
    BitmapFactory.decodeStream(bi);
    FileOutputStream fi = new FileOutputStream(outPath);
    fi.write(os.toByteArray());
    fi.flush();
    fi.close();

}

From source file:Main.java

public static String Base64Pic(String path) throws Exception {
    Bitmap bm = getSmallBitmap(path, 300, 300);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] toBytes(Node node, boolean formatted)
        throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writeNode(node, baos, formatted);// ww w. j av a 2s. c  o m
    return (baos.toByteArray());
}