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:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap colorDodgeBlend(Bitmap source, Bitmap layer) {
    Bitmap base = source.copy(Config.ARGB_8888, true);
    Bitmap blend = layer.copy(Config.ARGB_8888, false);

    IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight());
    base.copyPixelsToBuffer(buffBase);
    buffBase.rewind();//from   ww  w  .  ja va2 s. c o m

    IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight());
    blend.copyPixelsToBuffer(buffBlend);
    buffBlend.rewind();

    IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());
    buffOut.rewind();

    while (buffOut.position() < buffOut.limit()) {
        int filterInt = buffBlend.get();
        int srcInt = buffBase.get();

        int redValueFilter = Color.red(filterInt);
        int greenValueFilter = Color.green(filterInt);
        int blueValueFilter = Color.blue(filterInt);

        int redValueSrc = Color.red(srcInt);
        int greenValueSrc = Color.green(srcInt);
        int blueValueSrc = Color.blue(srcInt);

        int redValueFinal = colordodge(redValueFilter, redValueSrc);
        int greenValueFinal = colordodge(greenValueFilter, greenValueSrc);
        int blueValueFinal = colordodge(blueValueFilter, blueValueSrc);

        int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal);

        /*
         * float[] hsv = new float[3]; Color.colorToHSV(pixel, hsv); hsv[1] = 0.0f; float top =
         * VALUE_TOP; // Setting this as 0.95f gave the best result so far if (hsv[2] <= top) {
         * hsv[2] = 0.0f; } else { hsv[2] = 1.0f; } pixel = Color.HSVToColor(hsv);
         */

        buffOut.put(pixel);
    }

    buffOut.rewind();

    base.copyPixelsFromBuffer(buffOut);
    blend.recycle();

    return base;
}

From source file:org.ros.android.rviz_for_android.drawable.loader.ColladaLoader.java

private ETC1Texture compressBitmap(Bitmap uncompressedBitmap) {
    // Rescale the bitmap to be half its current size
    Bitmap uncompressedBitmapResize = Bitmap.createScaledBitmap(uncompressedBitmap,
            uncompressedBitmap.getWidth() / 4, uncompressedBitmap.getHeight() / 4, true);
    uncompressedBitmap.recycle();//  ww  w  .  ja v  a  2s.  c  o  m

    // Copy the bitmap to a byte buffer
    ByteBuffer uncompressedBytes = ByteBuffer.allocateDirect(uncompressedBitmapResize.getByteCount())
            .order(ByteOrder.nativeOrder());
    uncompressedBitmapResize.copyPixelsToBuffer(uncompressedBytes);
    uncompressedBytes.position(0);

    int width = uncompressedBitmapResize.getWidth();
    int height = uncompressedBitmapResize.getHeight();

    // Compress the texture
    int encodedSize = ETC1.getEncodedDataSize(width, height);
    ByteBuffer compressed = ByteBuffer.allocateDirect(encodedSize).order(ByteOrder.nativeOrder());
    ETC1.encodeImage(uncompressedBytes, width, height, 2, 2 * width, compressed);

    ETC1Texture retval = new ETC1Texture(width, height, compressed);

    // We're done with the uncompressed bitmap, release it
    uncompressedBitmapResize.recycle();

    return retval;
}

From source file:com.dalaran.LDCache.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override/* w w w.j  a  v  a  2  s .c o m*/
public void putBitmap(String url, Bitmap bitmap) {

    if (bitmap.getHeight() >= 100 && bitmap.getWidth() >= 100) {

        bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
    }

    Log.d("Disc cached:", url);
    final Cache.Entry entry = new Cache.Entry();

    try {
        ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(buffer);
        entry.data = buffer.array();
        put(url, entry);
        logicToPutBitmap(url, bitmap);
    } catch (Throwable e) {
        Log.w("Samsung 2.3.3", e.getMessage(), e);
    }
}

From source file:com.example.psumaps.MapView.java

public static Bitmap convertToMutable(Bitmap imgIn) {
    try {//from  w w w.j a va  2s. co 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.
        File file = new File(Environment.getExternalStorageDirectory() + 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();
        Bitmap.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(FileChannel.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 temp file
        file.delete();

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

    return imgIn;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap getCartoonizedBitmap(Bitmap realBitmap, Bitmap dodgeBlendBitmap, int hueIntervalSize,
        int saturationIntervalSize, int valueIntervalSize, int saturationPercent, int valuePercent) {
    // Bitmap bitmap = Bitmap.createBitmap(scaledBitmap);
    // //fastblur(scaledBitmap, 4);
    Bitmap base = fastblur(realBitmap, 3).copy(Config.ARGB_8888, true);
    Bitmap dodge = dodgeBlendBitmap.copy(Config.ARGB_8888, false);
    try {/*from  w ww.  j  a v a  2  s.  c  o  m*/
        int realColor;
        int color;
        float top = 0.87f;// VALUE_TOP; // Between 0.0f .. 1.0f I use 0.87f
        IntBuffer templatePixels = IntBuffer.allocate(dodge.getWidth() * dodge.getHeight());
        IntBuffer scaledPixels = IntBuffer.allocate(base.getWidth() * base.getHeight());
        IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());

        base.copyPixelsToBuffer(scaledPixels);
        dodge.copyPixelsToBuffer(templatePixels);

        templatePixels.rewind();
        scaledPixels.rewind();
        buffOut.rewind();

        while (buffOut.position() < buffOut.limit()) {
            color = (templatePixels.get());
            realColor = scaledPixels.get();

            float[] realHSV = new float[3];
            Color.colorToHSV(realColor, realHSV);

            realHSV[0] = getRoundedValue(realHSV[0], hueIntervalSize);

            realHSV[2] = (getRoundedValue(realHSV[2] * 100, valueIntervalSize) / 100) * (valuePercent / 100);
            realHSV[2] = realHSV[2] < 1.0 ? realHSV[2] : 1.0f;

            realHSV[1] = realHSV[1] * (saturationPercent / 100);
            realHSV[1] = realHSV[1] < 1.0 ? realHSV[1] : 1.0f;

            float[] HSV = new float[3];
            Color.colorToHSV(color, HSV);

            boolean putBlackPixel = HSV[2] <= top;

            realColor = Color.HSVToColor(realHSV);

            if (putBlackPixel) {
                buffOut.put(color);
            } else {
                buffOut.put(realColor);
            }
        } // END WHILE
        dodge.recycle();
        buffOut.rewind();
        base.copyPixelsFromBuffer(buffOut);

    } catch (Exception e) {
        // TODO: handle exception
    }

    return base;
}

From source file:org.mozilla.gecko.GeckoAppShell.java

public static byte[] getIconForExtension(String aExt, int iconSize) {
    try {/* ww  w  .  java  2  s.  c  om*/
        if (iconSize <= 0)
            iconSize = 16;

        if (aExt != null && aExt.length() > 1 && aExt.charAt(0) == '.')
            aExt = aExt.substring(1);

        PackageManager pm = GeckoApp.mAppContext.getPackageManager();
        Drawable icon = getDrawableForExtension(pm, aExt);
        if (icon == null) {
            // Use a generic icon
            icon = pm.getDefaultActivityIcon();
        }

        Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
        if (bitmap.getWidth() != iconSize || bitmap.getHeight() != iconSize)
            bitmap = Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, true);

        ByteBuffer buf = ByteBuffer.allocate(iconSize * iconSize * 4);
        bitmap.copyPixelsToBuffer(buf);

        return buf.array();
    } catch (Exception e) {
        Log.i(LOGTAG, "getIconForExtension error: ", e);
        return null;
    }
}