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

public static Bitmap copyBitmap(Bitmap bitmap) {
    return bitmap.copy(Config.ARGB_8888, true);
}

From source file:Main.java

public static final Bitmap copy(Bitmap bitmap) {
    return bitmap.copy(Config.ARGB_8888, true);
}

From source file:Main.java

public static Bitmap highlightSelectedFaceThumbnail(Bitmap originalBitmap) {
    Bitmap bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  w  ww  .j a v a 2 s.c  o m*/
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.parseColor("#3399FF"));
    int stokeWidth = Math.max(originalBitmap.getWidth(), originalBitmap.getHeight()) / 10;
    if (stokeWidth == 0) {
        stokeWidth = 1;
    }
    bitmap.getWidth();
    paint.setStrokeWidth(stokeWidth);
    canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);

    return bitmap;
}

From source file:Main.java

/**
 * Expand the bitmap to the specified scale.
 *
 * @param bitmap to expand.//from   w  w w  .  j a va  2s . co  m
 * @param scale  the expand scale, must be > 1.0.
 * @return the expanded bitmap.
 */
public static Bitmap expand(Bitmap bitmap, float scale) {
    if (scale <= 1.0f) {
        return bitmap.copy(bitmap.getConfig(), false);
    }

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    return Bitmap.createBitmap(bitmap, 0, 0, (int) (scale * bitmap.getWidth()),
            (int) (scale * bitmap.getHeight()), matrix, true);
}

From source file:Main.java

/**
 * Shrink the bitmap to the specified scale.
 *
 * @param bitmap to shrink./*from w  ww.j a  v  a 2  s  . com*/
 * @param scale  the shrink scale, must be < 1.0.
 * @return the shrunk bitmap.
 */
public static Bitmap shrink(Bitmap bitmap, float scale) {
    if (scale >= 1.0f) {
        return bitmap.copy(bitmap.getConfig(), false);
    }

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    return Bitmap.createBitmap(bitmap, 0, 0, (int) (scale * bitmap.getWidth()),
            (int) (scale * bitmap.getHeight()), matrix, true);
}

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);/*from w  w  w .ja  v  a  2  s .  c  o m*/

    FaceDetector.Face face = faces[0];
    if (face != null) {
        try {
            face.getMidPoint(mid);
            return mid;
        } catch (NullPointerException n) {
        }
    }
    return null;

}

From source file:Main.java

public static Drawable convertViewToDrawable(View view) {
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);//from  w  ww  . ja  va2 s  .  c  o  m
    view.layout(UPPER_LEFT_X, UPPER_LEFT_Y, view.getMeasuredWidth(), view.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(c);
    view.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    view.destroyDrawingCache();
    return new BitmapDrawable(viewBmp);
}

From source file:Main.java

public static Bitmap blurBitmap(Context context, Bitmap bitmap, float blur) {
    Bitmap bitmapCopy = bitmap.copy(bitmap.getConfig(), false);
    //Let's create an empty bitmap with the same size of the bitmap we want to blur
    Bitmap outBitmap = (Bitmap.createBitmap(bitmapCopy.getWidth(), bitmapCopy.getHeight(),
            Bitmap.Config.ARGB_8888));/* w  w  w . j a va  2s.  com*/

    //Instantiate a new Renderscript
    RenderScript rs = RenderScript.create(context);

    //Create an Intrinsic Blur Script using the Renderscript
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    //Create the in/out Allocations with the Renderscript and the in/out bitmaps
    Allocation allIn = Allocation.createFromBitmap(rs, bitmapCopy);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

    //Set the radius of the blur
    blurScript.setRadius(blur);

    //Perform the Renderscript
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    //Copy the final bitmap created by the out Allocation to the outBitmap
    allOut.copyTo(outBitmap);

    bitmapCopy.recycle();
    //After finishing everything, we destroy the Renderscript.
    rs.destroy();
    return outBitmap;
}

From source file:Main.java

/**
 * This method updates the opacity of the bitmap.
 *
 * @param bitmap  The bitmap.//from  ww w  .j  a v a2  s. co  m
 * @param opacity The value of alpha.
 * @return The bitmap after adjusting the opacity.
 */
public static Bitmap adjustOpacity(Bitmap bitmap, int opacity) {

    Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int color = (opacity & 0xFF) << 24;
    canvas.drawColor(color, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

From source file:Main.java

/**
 * Method to scale {@code sourceBitmap}, maintaining the same original size of the bitmap,
 * but with a transparent frame and the scaled and centered {@code sourceBitmap} inside.
 *
 * @return/*  ww  w .j  a  v a2 s .c om*/
 */
public static Bitmap scaleInsideWithFrame(Bitmap mutableBitmap, float factor, int color) {
    Bitmap clearBitmap = mutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
    clearBitmap.eraseColor(color);

    Bitmap resizedInsideBitmap = scaleBitmapByFactor(mutableBitmap, factor);

    int frameWidth = clearBitmap.getWidth();
    int frameHeight = clearBitmap.getHeight();
    int imageWidth = resizedInsideBitmap.getWidth();
    int imageHeight = resizedInsideBitmap.getHeight();

    Canvas canvas = new Canvas(clearBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    canvas.drawBitmap(resizedInsideBitmap, (frameWidth - imageWidth) / 2, (frameHeight - imageHeight) / 2,
            paint);
    return clearBitmap;
}