Example usage for android.graphics Bitmap copyPixelsToBuffer

List of usage examples for android.graphics Bitmap copyPixelsToBuffer

Introduction

In this page you can find the example usage for android.graphics Bitmap copyPixelsToBuffer.

Prototype

public void copyPixelsToBuffer(Buffer dst) 

Source Link

Document

Copy the bitmap's pixels into the specified buffer (allocated by the caller).

Usage

From source file:Main.java

/**
 * Converts bitmap to the byte array without compression
 * @param bitmap source bitmap//from  w  w  w  . ja  va  2  s.  c o m
 * @return result byte array
 */
public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
    bitmap.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();
    return byteBuffer.array();
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    int bytes = bitmap.getByteCount();
    ByteBuffer buf = ByteBuffer.allocate(bytes);
    bitmap.copyPixelsToBuffer(buf);
    byte[] byteArray = buf.array();
    return byteArray;
}

From source file:Main.java

public static String convertBitmapToBase64(Bitmap bitmap) {
    final int size = bitmap.getByteCount();

    ByteBuffer dst = ByteBuffer.allocate(size);
    bitmap.copyPixelsToBuffer(dst);
    return Base64.encodeToString(dst.array(), Base64.DEFAULT);
}

From source file:Main.java

public static byte[] getBytes(Bitmap bitmap) {
    int byteCount = bitmap.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(byteCount);
    bitmap.copyPixelsToBuffer(buffer);
    buffer.rewind();/*ww w. ja v  a  2 s . c om*/

    byte[] data = new byte[byteCount];
    buffer.get(data);
    return data;
}

From source file:Main.java

public static InputStream bitmapToInputStream(Bitmap bitmap) {
    int size = bitmap.getHeight() * bitmap.getRowBytes();
    ByteBuffer buffer = ByteBuffer.allocate(size);
    bitmap.copyPixelsToBuffer(buffer);
    return new ByteArrayInputStream(buffer.array());
}

From source file:Main.java

public static byte[] bitmapToArray(Bitmap b) {
    int bytes = b.getByteCount();

    ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
    b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

    byte[] array = buffer.array(); //Get the underlying array containing the data.

    return array;
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    //int numBytes = bitmap.getWidth() + bitmap.getHeight() * 4;
    int numBytes = bitmap.getByteCount();
    ByteBuffer byteBuffer = ByteBuffer.allocate(numBytes);
    bitmap.copyPixelsToBuffer(byteBuffer);
    return byteBuffer.array();
}

From source file:Main.java

/**
 * //from   ww  w . j  a  va  2s .c  o m
 * @return Bitmap's RGBA byte array
 */
public static byte[] getImageRGBA(Bitmap inputBitmap) {
    Config config = inputBitmap.getConfig();
    ByteBuffer buffer;

    Bitmap bitmap;
    /**
     * if bitmap size is not 32*32 create scaled bitmap
     */

    if (inputBitmap.getWidth() != 32 || inputBitmap.getHeight() != 32) {
        Log.d(TAG, "bitmap resized to 32x32");
        bitmap = Bitmap.createScaledBitmap(inputBitmap, 32, 32, false);
    } else {
        bitmap = inputBitmap;
    }
    /**
     * if bitmap is not ARGB_8888 format, copy bitmap with ARGB_8888 format
     */
    if (!config.equals(Bitmap.Config.ARGB_8888)) {
        Bitmap bitmapARBG = bitmap.copy(Bitmap.Config.ARGB_8888, false);
        buffer = ByteBuffer.allocate(bitmapARBG.getByteCount());
        bitmapARBG.copyPixelsToBuffer(buffer);
        bitmapARBG.recycle();
    } else {
        buffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(buffer);
    }
    return buffer.array();
}

From source file:Main.java

/**
 * Finds the percentage of pixels that do are empty.
 *
 * @param bitmap input bitmap//from   w w  w  . j  a v a2s  .c  om
 * @return a value between 0.0 to 1.0 . Note the method will return 0.0 if either of bitmaps are null nor of same size.
 *
 */
public static float getTransparentPixelPercent(Bitmap bitmap) {

    if (bitmap == null) {
        return 0f;
    }

    ByteBuffer buffer = ByteBuffer.allocate(bitmap.getHeight() * bitmap.getRowBytes());
    bitmap.copyPixelsToBuffer(buffer);

    byte[] array = buffer.array();

    int len = array.length;
    int count = 0;

    for (int i = 0; i < len; i++) {
        if (array[i] == 0) {
            count++;
        }
    }

    return ((float) (count)) / len;
}

From source file:Main.java

public static byte[] getBytePixels(final Bitmap bit) {
    if (bit == null) {
        return null;
    }//w w w. j a v  a  2 s  .com
    final byte[] pixels = new byte[bit.getWidth() * bit.getHeight() * 4];
    final ByteBuffer buf = ByteBuffer.wrap(pixels);
    buf.order(ByteOrder.nativeOrder());
    bit.copyPixelsToBuffer(buf);
    return pixels;
}