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

public static byte[] toByteArrayNew(Bitmap source) {
    //int size = source.getRowBytes() * source.getHeight();
    int size = source.getByteCount();
    ByteBuffer byteBuffer = ByteBuffer.allocate(size);
    source.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();/* www  .  j a  v a2s. c  o m*/
    byte[] b = byteBuffer.array();
    return b;
}

From source file:io.card.development.recording.ManifestEntry.java

private static byte[] decompress(byte[] compressed) {
    /*/*  w  w  w  .  ja v a  2s  .c o m*/
     * DO NOT EVER USE THIS METHOD IN PRODUCTION This is horribly inefficient, but written only
     * for testing purposes.
     */

    Bitmap b = BitmapFactory.decodeByteArray(compressed, 0, compressed.length);
    ByteBuffer bb = ByteBuffer.allocate(b.getWidth() * b.getHeight() * 4);
    b.copyPixelsToBuffer(bb);
    b.recycle();

    byte[] ba = bb.array();
    byte[] singleChannel = new byte[ba.length / 4]; // 4 channels
    for (int i = 0; i < singleChannel.length; i++) {
        singleChannel[i] = ba[i * 4 + 1];
    }

    return singleChannel;
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap b) {
    //calculate how many bytes our image consists of.
    int bytes = b.getByteCount();
    //or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
    //int bytes = b.getWidth()*b.getHeight()*4;

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

    byte[] array = buffer.array();

    return array;
}

From source file:Main.java

/**
 * Compares two bitmaps and gives the percentage of similarity
 *
 * @param bitmap1 input bitmap 1/*w w w  .ja  v  a 2s  .  c o  m*/
 * @param bitmap2 input bitmap 2
 * @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 compareEquivalance(Bitmap bitmap1, Bitmap bitmap2) {

    if (bitmap1 == null || bitmap2 == null || bitmap1.getWidth() != bitmap2.getWidth()
            || bitmap1.getHeight() != bitmap2.getHeight()) {
        return 0f;
    }

    ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight() * bitmap1.getRowBytes());
    bitmap1.copyPixelsToBuffer(buffer1);

    ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight() * bitmap2.getRowBytes());
    bitmap2.copyPixelsToBuffer(buffer2);

    byte[] array1 = buffer1.array();
    byte[] array2 = buffer2.array();

    int len = array1.length; // array1 and array2 will be of some length.
    int count = 0;

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

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

From source file:org.cocos2dx.lib.Cocos2dxBitmap.java

private static byte[] getPixels(Bitmap bitmap) {
    if (bitmap != null) {
        byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight() * 4];
        ByteBuffer buf = ByteBuffer.wrap(pixels);
        buf.order(ByteOrder.nativeOrder());
        bitmap.copyPixelsToBuffer(buf);
        return pixels;
    }/*  www  .  j ava 2s .  c om*/

    return null;
}

From source file:Main.java

public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) {
    Canvas c = new Canvas();
    Paint p = new Paint();
    float density = context.getResources().getDisplayMetrics().density;
    //        Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s density=%f", font, code, density));
    p.setTextSize((float) size * density);
    p.setAntiAlias(true);/*from  ww  w.  j a v  a2  s . com*/

    Rect textBounds = new Rect();
    p.getTextBounds(code, 0, code.length(), textBounds);
    //        Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom));

    Rect textBoundsAxA = new Rect();
    String axa = String.format("A%sA", code);
    p.getTextBounds(axa, 0, axa.length(), textBoundsAxA);
    Rect textBoundsAA = new Rect();
    String aa = "AA";
    p.getTextBounds(aa, 0, aa.length(), textBoundsAA);

    // cache.distDelta = Vec2(0, 0);
    arrayOfPos[0] = textBounds.left;
    arrayOfPos[1] = textBounds.top;

    // cache.srcWidth = Vec2(16, 16);
    arrayOfPos[2] = textBounds.width();
    arrayOfPos[3] = textBounds.height();

    // cache.step = 16;
    //      arrayOfPos[4] = textBounds.width() + 1;
    arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width();

    if (textBounds.width() == 0 || textBounds.height() == 0) {
        Log.v(TAG, "makeFontBitmap: empty");
        return null;
    }

    Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888);
    c.setBitmap(b);

    Rect r = new Rect(0, 0, textBounds.width(), textBounds.height());
    //      p.setColor(Color.RED);
    p.setARGB(0, 0, 0, 0);
    c.drawRect(r, p);
    p.setARGB(255, 255, 255, 255);

    //        Log.v(TAG, "makeFontBitmap: drawText");
    c.drawText(code, -textBounds.left, -textBounds.top, p);
    //        Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3]));

    ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4);
    //        Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes()));
    buf.position(0);
    b.copyPixelsToBuffer(buf);
    //        Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity()));

    return buf.array();
}

From source file:Main.java

public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) {
    Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s", font, code));
    Canvas c = new Canvas();
    Paint p = new Paint();
    //        Log.v(TAG, "get density");
    float density = context.getResources().getDisplayMetrics().density;
    Log.v(TAG, String.format("makeFontBitmap density: %f", density));
    p.setTextSize((float) size * density);
    p.setAntiAlias(true);//from   ww  w . ja  v  a 2 s. com

    Rect textBounds = new Rect();
    p.getTextBounds(code, 0, code.length(), textBounds);
    Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top,
            textBounds.right, textBounds.bottom));

    Rect textBoundsAxA = new Rect();
    String axa = String.format("A%sA", code);
    p.getTextBounds(axa, 0, axa.length(), textBoundsAxA);
    Rect textBoundsAA = new Rect();
    String aa = "AA";
    p.getTextBounds(aa, 0, aa.length(), textBoundsAA);

    // cache.distDelta = Vec2(0, 0);
    arrayOfPos[0] = textBounds.left;
    arrayOfPos[1] = textBounds.top;

    // cache.srcWidth = Vec2(16, 16);
    arrayOfPos[2] = textBounds.width();
    arrayOfPos[3] = textBounds.height();

    // cache.step = 16;
    //      arrayOfPos[4] = textBounds.width() + 1;
    arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width();

    if (textBounds.width() == 0 || textBounds.height() == 0) {
        Log.v(TAG, "makeFontBitmap: empty");
        return null;
    }

    Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888);
    c.setBitmap(b);

    Rect r = new Rect(0, 0, textBounds.width(), textBounds.height());
    //      p.setColor(Color.RED);
    p.setARGB(0, 0, 0, 0);
    c.drawRect(r, p);
    p.setARGB(255, 255, 255, 255);

    //        Log.v(TAG, "makeFontBitmap: drawText");
    c.drawText(code, -textBounds.left, -textBounds.top, p);
    Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3]));

    ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4);
    //      ByteBuffer buf = ByteBuffer.allocate(b.getRowBytes());
    Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes()));
    buf.position(0);
    b.copyPixelsToBuffer(buf);
    Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity()));

    //      byte bytes[] = buf.array();
    //      for (int i = 0; i < size * size * 2; i++)
    //         bytes[i] = (byte)(Math.random() * 255);
    return buf.array();
}

From source file:Main.java

public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) {
    try {//w  w  w . j a  v  a 2s  .c  o  m
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        int index = tempFileName.lastIndexOf(".");
        if (index != -1)
            tempFileName = tempFileName.substring(0, index);
        File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();
        Config type = srcBitmap.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height);
        srcBitmap.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        srcBitmap.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        srcBitmap = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        srcBitmap.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temp file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return srcBitmap;
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

/**
 * Source:// w ww  . j a v  a 2  s  .c o m
 * http://stackoverflow.com/questions/4349075/bitmapfactory-decoderesource
 * -returns-a-mutable-bitmap-in-android-2-2-and-an-immu
 * 
 * Converts a immutable bitmap to a mutable bitmap. This operation doesn't
 * allocates more memory that there is already allocated.
 * 
 * @param imgIn
 *            - Source image. It will be released, and should not be used
 *            more
 * @return a copy of imgIn, but immutable.
 */
public static Bitmap convertBitmapToMutable(Bitmap imgIn) {
    try {
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        File file = new File(MyApp.context.getFilesDir() + File.separator + "temp.tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = imgIn.getWidth();
        int height = imgIn.getHeight();
        Config type = imgIn.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
        imgIn.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        imgIn.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        imgIn = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        imgIn.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temporary file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return imgIn;
}

From source file:org.mozilla.gecko.gfx.PlaceholderLayerClient.java

boolean loadScreenshot() {
    if (GeckoApp.mAppContext.mLastScreen == null)
        return false;
    Bitmap bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(GeckoApp.mAppContext.mLastScreen));
    if (bitmap == null)
        return false;

    Bitmap.Config config = bitmap.getConfig();

    mWidth = bitmap.getWidth();//from  w w  w.ja v  a 2s  .  c o  m
    mHeight = bitmap.getHeight();
    mFormat = CairoUtils.bitmapConfigToCairoFormat(config);

    int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;
    mBuffer = GeckoAppShell.allocateDirectBuffer(mWidth * mHeight * bpp);

    bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer());

    if (mViewportUnknown) {
        mViewport.setPageSize(new FloatSize(mWidth, mHeight));
        if (getLayerController() != null)
            getLayerController().setPageSize(mViewport.getPageSize());
    }

    return true;
}