Example usage for android.graphics Bitmap copy

List of usage examples for android.graphics Bitmap copy

Introduction

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

Prototype

public Bitmap copy(Config config, boolean isMutable) 

Source Link

Document

Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap.

Usage

From source file:Main.java

/**
 * TODO write documentation// w w w . j a v a 2  s  . c o  m
 *
 * @param sourceBitmap
 * @param color
 * @return
 */
public static Bitmap overlayColor(Bitmap sourceBitmap, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),
            sourceBitmap.getHeight());
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);
    return mutableBitmap;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap fastBlur(Context context, Bitmap bm, int radius) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Bitmap bitmap = bm.copy(bm.getConfig(), true);
        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs, bm, Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
        final Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setRadius(radius);//  w  w w .  ja v  a  2  s  .  c o m
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);

        // clean up renderscript resources
        rs.destroy();
        input.destroy();
        output.destroy();
        script.destroy();

        return bitmap;
    }
    return null;
}

From source file:Main.java

public static Bitmap effectChangeContrast(Bitmap bitmap, double effectLevel) {
    if (bitmap == null) {
        return bitmap;
    }//www  . j  a  v  a2s . c o m

    Bitmap dst = bitmap.copy(Bitmap.Config.ARGB_8888, true);

    int height = dst.getHeight();
    int width = dst.getWidth();
    int[] pixels = new int[(width * height)];
    dst.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int YY = 0; YY < width; ++YY) {
        for (int XX = 0; XX < height; ++XX) {
            int bitmapColor = pixels[(YY + XX * width)];
            int[] color = { Color.red(bitmapColor), Color.green(bitmapColor), Color.blue(bitmapColor) };

            if (color[0] > 200 && color[1] > 200 && color[3] > 200) {
                color[0] = 255;
                color[1] = 255;
                color[2] = 255;
            }

            for (int i = 0; i < color.length; i++) {
                int tmpColor = color[i];
                tmpColor = (int) ((tmpColor - 128) * effectLevel + 128);
                if (tmpColor < 0) {
                    tmpColor = 0;
                } else if (tmpColor > 255) {
                    tmpColor = 255;
                }
                color[i] = tmpColor;
            }

            pixels[(YY + XX * width)] = Color.rgb(color[0], color[1], color[2]);
        }
    }

    dst.setPixels(pixels, 0, width, 0, 0, width, height);
    return dst;
}

From source file:Main.java

private static PointF findFaceMid(Bitmap in) {
    PointF mid = new PointF();
    Bitmap bitmap565 = in.copy(Bitmap.Config.RGB_565, true);

    FaceDetector fd = new FaceDetector(in.getWidth(), in.getHeight(), 1);
    FaceDetector.Face[] faces = new FaceDetector.Face[1];
    fd.findFaces(bitmap565, faces);/* w w w  .j  a  v a  2s  .co m*/

    FaceDetector.Face face = faces[0];
    if (face != null) {
        try {
            face.getMidPoint(mid);
            return mid;
        } catch (NullPointerException n) {
            // FIXME please fix this horrible NPE catch block
            Log.e("Error", n.getLocalizedMessage());
        }
    }
    return null;

}

From source file:Main.java

/**
 * Method to create a fully-transparent Bitmap using the same size of the source passed as
 * parameter and also the same density.//  w ww  . j  a v  a2s  .com
 *
 * @param source The original Bitmap.
 * @return A transparent Bitmap with the same size of the source.
 */
public static Bitmap clear(Bitmap source) {
    Bitmap newBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight());
    //to erase the color from a Bitmap, I must use a mutable Bitmap!!!!
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
    mutableBitmap.eraseColor(Color.TRANSPARENT);
    return mutableBitmap;
}

From source file:Main.java

/**
 * Convert the Bitmap to ALPHA_8.  The old one will be recycled.
 *//*from   ww w  .j  a v  a  2 s  .c  o  m*/
static Bitmap toAlpha8(Bitmap src, boolean recycle) {
    if (!src.hasAlpha()) {
        src = redToAlpha(src, recycle);
        recycle = true;
    }

    Bitmap dest = src.copy(Bitmap.Config.ALPHA_8, false);
    if (recycle)
        src.recycle();
    return dest;
}

From source file:Main.java

public static Bitmap drawImageBorder(Bitmap srcBitmap, Bitmap borderBitmap) {
    if (srcBitmap == null)
        throw new NullPointerException("srcBitmap should not null");
    if (borderBitmap == null)
        return srcBitmap;
    Bitmap temp = srcBitmap.copy(Config.ARGB_8888, true);
    /*Bitmap border = borderBitmap;
    if (srcBitmap.getWidth() != borderBitmap.getWidth() || 
      srcBitmap.getHeight() != borderBitmap.getHeight()) {
       border = zoomBitmap(borderBitmap, srcBitmap.getWidth(), srcBitmap.getHeight());
    }*///from   w w w  . j  a v  a2  s . c  o  m
    Canvas canvas = new Canvas(temp);
    Matrix m = new Matrix();
    m.postScale((float) temp.getWidth() / borderBitmap.getWidth(),
            (float) temp.getHeight() / borderBitmap.getHeight());
    //canvas.drawBitmap(border, 0, 0, null);
    canvas.drawBitmap(borderBitmap, m, null);
    return temp;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context context, int resId, String text) {

    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null)
        bitmapConfig = Bitmap.Config.ARGB_8888;
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(context.getResources().getColor(android.R.color.white));
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    paint.setTextSize((int) (12 * scale));

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 4;
    int y = (bitmap.getHeight() + bounds.height()) / 5;

    canvas.drawText(text, x * scale, y * scale, paint);

    return bitmap;
}

From source file:Main.java

/**
 * ---------------------------------------------------------------------------
 * USE THIS METHOD AND NOT THE OLDER VERSION CALLED: "overlayColorOnGrayScale"
 * ---------------------------------------------------------------------------
 * Method to overlay color on a gray scale Bitmap.
 * This method creates automatically a gray scale bitmap from {@code source}.
 *
 * @param source The original colored Bitmap.
 * @param color  Color to overlay.//from  w w w. j  a va 2s  .c  o  m
 * @return A colored gray scale Bitmap.
 */
public static Bitmap overlayColorOnGrayScale(Bitmap source, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Canvas canvas = new Canvas(mutableBitmap);
    canvas.drawBitmap(source, 0, 0, getGrayScalePaint());

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    return mutableBitmap;
}

From source file:Main.java

public static Bitmap fastblur(Bitmap sentBitmap, int w, int h, int radius) {
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    fastblur(sentBitmap, bitmap, w, h, radius);
    return bitmap;
}