Java Code Examples for android.graphics.Bitmap#copy()

The following examples show how to use android.graphics.Bitmap#copy() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private Bitmap drawVipType(UserModel model, Bitmap bitmap) {
	if (!model.verified || model.verified_type < 0) return bitmap;
	
	BitmapDrawable drawable = mVipDrawable[model.verified_type > 1 ? 1 : model.verified_type];
	Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
	Canvas canvas = new Canvas(copy);
	int w1 = bitmap.getWidth();
	int w2 = w1 / 4;
	int h1 = bitmap.getHeight();
	int h2 = h1 / 4;
	drawable.setBounds(w1 - w2, h1 - h2, w1, h1);
	drawable.draw(canvas);
	
	bitmap.recycle();
	
	return copy;
}
 
Example 2
@Override
protected Bitmap blur(Context context, Bitmap bitmapToBlur, int radius) {
    Log.i(TAG, "Current build version sdk " + Build.VERSION.SDK_INT);
    Bitmap bitmap = bitmapToBlur.copy(bitmapToBlur.getConfig(), true);

    final RenderScript renderScript = RenderScript.create(context, Build.VERSION.SDK_INT);
    final Allocation input = Allocation.createFromBitmap(renderScript, bitmapToBlur,
                                                            Allocation.MipmapControl.MIPMAP_NONE,
                                                            Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(renderScript, input.getType());
    try {
        final ScriptIntrinsicBlur script = createBlurringScript(radius, renderScript, input);
        script.forEach(output);
        renderScript.finish();
        output.copyTo(bitmap);
    } finally {
        input.destroy();
        output.destroy();
        bitmapToBlur.recycle();
        renderScript.destroy();
    }
    return bitmap;
}
 
Example 3
Source Project: SimplifyReader   File: ImageBlurManager.java    License: Apache License 2.0 6 votes vote down vote up
public static Bitmap doBlurJniBitMap(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
    Bitmap bitmap;
    if (canReuseInBitmap) {
        bitmap = sentBitmap;
    } else {
        bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    }

    if (radius < 1) {
        return (null);
    }
    // Jni bitmap calculate
    ImageBlur.blurBitMap(bitmap, radius);

    return (bitmap);
}
 
Example 4
Source Project: PracticeDemo   File: FastBlurActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 有些手机不支持....so  不能用
 *
 * @param sentBitmap
 * @param radius
 */
private void RenderScriptblur(Bitmap sentBitmap, int radius) {

    long start = System.currentTimeMillis();

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    final RenderScript rs = RenderScript.create(FastBlurActivity.this);
    final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius /* e.g. 3.f */);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);

    mIvBlur.setImageBitmap(bitmap);
    long end = System.currentTimeMillis();

    Log.v(TAG, "fastblur time:" + (end - start));
}
 
Example 5
@Override
public Bitmap transform(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    setDimensions(width, height);
    
    Bitmap bitmap = source.copy(source.getConfig(), true);
    source.recycle();

    int[] inPixels = new int[width];
    for (int y = 0; y < height; y++) {
        bitmap.getPixels(inPixels, 0, width, 0, y, width, 1);
        for (int x = 0; x < width; x++) {
            inPixels[x] = filterRGB(x, y, inPixels[x]);
        }
        bitmap.setPixels(inPixels, 0, width, 0, y, width, 1);
    }

    return bitmap;
}
 
Example 6
Source Project: AndroidUtilCode   File: ImageUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return the bitmap with image watermarking.
 *
 * @param src       The source of bitmap.
 * @param watermark The image watermarking.
 * @param x         The x coordinate of the first pixel.
 * @param y         The y coordinate of the first pixel.
 * @param alpha     The alpha of watermark.
 * @param recycle   True to recycle the source of bitmap, false otherwise.
 * @return the bitmap with image watermarking
 */
public static Bitmap addImageWatermark(final Bitmap src,
                                       final Bitmap watermark,
                                       final int x,
                                       final int y,
                                       final int alpha,
                                       final boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    Bitmap ret = src.copy(src.getConfig(), true);
    if (!isEmptyBitmap(watermark)) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Canvas canvas = new Canvas(ret);
        paint.setAlpha(alpha);
        canvas.drawBitmap(watermark, x, y, paint);
    }
    if (recycle && !src.isRecycled() && ret != src) src.recycle();
    return ret;
}
 
Example 7
Source Project: Android-utils   File: ImageUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static Bitmap addBorder(final Bitmap src,
                                @IntRange(from = 1) final int borderSize,
                                @ColorInt final int color,
                                final boolean isCircle,
                                final float cornerRadius,
                                final boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
    int width = ret.getWidth();
    int height = ret.getHeight();
    Canvas canvas = new Canvas(ret);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(borderSize);
    if (isCircle) {
        float radius = Math.min(width, height) / 2f - borderSize / 2f;
        canvas.drawCircle(width / 2f, height / 2f, radius, paint);
    } else {
        int halfBorderSize = borderSize >> 1;
        RectF rectF = new RectF(halfBorderSize, halfBorderSize,
                width - halfBorderSize, height - halfBorderSize);
        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
    }
    return ret;
}
 
Example 8
protected BitmapDrawable convertViewToDrawable(View textView) {
    int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getMeasuredWidth(), textView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(c);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();
    BitmapDrawable bd = new BitmapDrawable(getContext().getResources(), viewBmp);
    bd.setBounds(0, 0, viewBmp.getWidth(), viewBmp.getHeight());

    return bd;
}
 
Example 9
Source Project: Android-utils   File: ImageUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Bitmap addImageWatermark(final Bitmap src,
                                       final Bitmap watermark,
                                       final int x,
                                       final int y,
                                       final int alpha,
                                       final boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    Bitmap ret = src.copy(src.getConfig(), true);
    if (!isEmptyBitmap(watermark)) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Canvas canvas = new Canvas(ret);
        paint.setAlpha(alpha);
        canvas.drawBitmap(watermark, x, y, paint);
    }
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 10
@Override
public Bitmap transform(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap bitmap = source.copy(source.getConfig(), true);
    source.recycle();

    originalSpace = new Rect(0, 0, width, height);
    transformedSpace = new Rect(0, 0, width, height);
    transformSpace(transformedSpace);

    int[] inPixels = new int[width * height];
    bitmap.getPixels(inPixels, 0, width, 0, 0, width, height);
    inPixels = filterPixels(width, height, inPixels, transformedSpace);

    bitmap.setPixels(inPixels, 0, transformedSpace.width(), 0, 0, transformedSpace.width(),
            transformedSpace.height());

    return bitmap;
}
 
Example 11
Source Project: AndroidUtilCode   File: ImageUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the bitmap with border.
 *
 * @param src          The source of bitmap.
 * @param borderSize   The size of border.
 * @param color        The color of border.
 * @param isCircle     True to draw circle, false to draw corner.
 * @param cornerRadius The radius of corner.
 * @param recycle      True to recycle the source of bitmap, false otherwise.
 * @return the bitmap with border
 */
private static Bitmap addBorder(final Bitmap src,
                                @IntRange(from = 1) final int borderSize,
                                @ColorInt final int color,
                                final boolean isCircle,
                                final float cornerRadius,
                                final boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
    int width = ret.getWidth();
    int height = ret.getHeight();
    Canvas canvas = new Canvas(ret);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(borderSize);
    if (isCircle) {
        float radius = Math.min(width, height) / 2f - borderSize / 2f;
        canvas.drawCircle(width / 2f, height / 2f, radius, paint);
    } else {
        int halfBorderSize = borderSize >> 1;
        RectF rectF = new RectF(halfBorderSize, halfBorderSize,
                width - halfBorderSize, height - halfBorderSize);
        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
    }
    return ret;
}
 
Example 12
@SuppressLint("NewApi")
private Bitmap getClusterBitmap(Resources res, int resourceId, int clusterSize) {
	BitmapFactory.Options options = new BitmapFactory.Options();

	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		options.inMutable = true;
	}
	Bitmap bitmap = BitmapFactory.decodeResource(res, resourceId, options);
	if (bitmap.isMutable() == false) {
		bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
	}

	Canvas canvas = new Canvas(bitmap);

	Paint paint = null;
	float originY;
	if (clusterSize < 100) {
		paint = clusterPaintLarge;
		originY = bitmap.getHeight() * 0.64f;
	} else if (clusterSize < 1000) {
		paint = clusterPaintMedium;
		originY = bitmap.getHeight() * 0.6f;
	} else {
		paint = clusterPaintSmall;
		originY = bitmap.getHeight() * 0.56f;
	}

	canvas.drawText(String.valueOf(clusterSize), bitmap.getWidth() * 0.5f, originY, paint);

	return bitmap;
}
 
Example 13
private static Bitmap ensureGLCompatibleBitmap(Bitmap bitmap) {
    if (bitmap == null || bitmap.getConfig() != null) {
        return bitmap;
    }
    Bitmap newBitmap = bitmap.copy(Config.ARGB_8888, false);
    bitmap.recycle();
    return newBitmap;
}
 
Example 14
Source Project: Paddle-Lite-Demo   File: Predictor.java    License: Apache License 2.0 5 votes vote down vote up
public void setInputImage(Bitmap image) {
    if (image == null) {
        return;
    }
    // Scale image to the size of input tensor
    Bitmap rgbaImage = image.copy(Bitmap.Config.ARGB_8888, true);
    Bitmap scaleImage = Bitmap.createScaledBitmap(rgbaImage, (int) inputShape[3], (int) inputShape[2], true);
    this.inputImage = scaleImage;
}
 
Example 15
Source Project: RairDemo   File: FaceUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Bitmap detectionFace(Bitmap b) {
    // 检测前必须转化为RGB_565格式。文末有详述连接
    Bitmap bitmap = b.copy(Bitmap.Config.RGB_565, true);
    b.recycle();
    // 设定最大可查的人脸数量
    int MAX_FACES = 5;
    FaceDetector faceDet = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), MAX_FACES);
    // 将人脸数据存储到faceArray 中
    FaceDetector.Face[] faceArray = new FaceDetector.Face[MAX_FACES];
    // 返回找到图片中人脸的数量,同时把返回的脸部位置信息放到faceArray中,过程耗时
    int findFaceCount = faceDet.findFaces(bitmap, faceArray);
    // 获取传回的脸部数组中的第一张脸的信息
    FaceDetector.Face face1 = faceArray[0];
    // 获取双眼的中心点,用一个PointF来接收其x、y坐标
    PointF point = new PointF();
    face1.getMidPoint(point);
    // 获取该部位为人脸的可信度,0~1
    float confidence = face1.confidence();
    // 获取双眼间距
    float eyesDistance = face1.eyesDistance();
    // 获取面部姿势
    // 传入X则获取到x方向上的角度,传入Y则获取到y方向上的角度,传入Z则获取到z方向上的角度
    float angle = face1.pose(FaceDetector.Face.EULER_X);

    // todo 在bitmap上绘制一个Rect框住脸,因为返回的是眼睛位置,所以还要做一些处理

    return bitmap;
}
 
Example 16
public static Bitmap copyBitmap(Bitmap bitmap){
    return bitmap.copy(bitmap.getConfig(),true);
}
 
Example 17
Source Project: LLApp   File: RoundWhiteImageView.java    License: Apache License 2.0 4 votes vote down vote up
@Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        this.measure(0, 0);
        if (drawable.getClass() == NinePatchDrawable.class)
            return;
//        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap b = drawableToBitamp(drawable);
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        if (defaultWidth == 0) {
            defaultWidth = getWidth();
        }
        if (defaultHeight == 0) {
            defaultHeight = getHeight();
        }
        // 保证重新读取图片后不会因为图片大小而改变控件宽、高的大小(针对宽、高为wrap_content布局的imageview,但会导致margin无效)
        // if (defaultWidth != 0 && defaultHeight != 0) {
        // LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        // defaultWidth, defaultHeight);
        // setLayoutParams(params);
        // }

        int radius = 0;
        if (mBorderInsideColor != defaultColor
                && mBorderOutsideColor != defaultColor) {// 定义画两个边框,分别为外圆边框和内圆边框
            radius = (defaultWidth < defaultHeight ? defaultWidth
                    : defaultHeight) / 2 - 2 * mBorderThickness;
            // 画内圆
            drawCircleBorder(canvas, radius + mBorderThickness / 2,
                    mBorderInsideColor);
            // 画外圆
            drawCircleBorder(canvas, radius + mBorderThickness
                    + mBorderThickness / 2, mBorderOutsideColor);
        } else if (mBorderInsideColor != defaultColor
                && mBorderOutsideColor == defaultColor) {// 定义画一个边框
            radius = (defaultWidth < defaultHeight ? defaultWidth
                    : defaultHeight) / 2 - mBorderThickness;
            drawCircleBorder(canvas, radius + mBorderThickness / 2,
                    mBorderInsideColor);
        } else if (mBorderInsideColor == defaultColor
                && mBorderOutsideColor != defaultColor) {// 定义画一个边框
            radius = (defaultWidth < defaultHeight ? defaultWidth
                    : defaultHeight) / 2 - mBorderThickness;
            drawCircleBorder(canvas, radius + mBorderThickness / 2,
                    mBorderOutsideColor);
        } else {// 没有边框
            radius = (defaultWidth < defaultHeight ? defaultWidth
                    : defaultHeight) / 2;
        }
        Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius);
        canvas.drawBitmap(roundBitmap, defaultWidth / 2 - radius, defaultHeight
                / 2 - radius, null);
    }
 
Example 18
Source Project: auid2   File: CaptionedImageView.java    License: Apache License 2.0 4 votes vote down vote up
private void updateBlur() {
    if (!(mDrawable instanceof BitmapDrawable)) {
        return;
    }
    final int textViewHeight = mTextView.getHeight();
    if (textViewHeight == 0) {
        return;
    }

    // Determine the size of the TextView compared to the height of the ImageView
    final float ratio = (float) textViewHeight / mImageView.getHeight();

    // Get the Bitmap
    final BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
    final Bitmap originalBitmap = bitmapDrawable.getBitmap();

    // Calculate the height as a ratio of the Bitmap
    int height = (int) (ratio * originalBitmap.getHeight());

    // The y position is the number of pixels height represents from the bottom of the Bitmap
    final int y = originalBitmap.getHeight() - height;

    final Bitmap portionToBlur = Bitmap.createBitmap(originalBitmap, 0, y, originalBitmap.getWidth(), height);
    final Bitmap blurredBitmap = portionToBlur.copy(Bitmap.Config.ARGB_8888, true);

    // Use RenderScript to blur the pixels
    RenderScript rs = RenderScript.create(getContext());
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, portionToBlur);
    Allocation tmpOut = Allocation.createFromBitmap(rs, blurredBitmap);
    theIntrinsic.setRadius(25f);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(blurredBitmap);
    new Canvas(blurredBitmap).drawColor(mScrimColor);

    // Create the new bitmap using the old plus the blurred portion and display itwww
    final Bitmap newBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
    final Canvas canvas = new Canvas(newBitmap);
    canvas.drawBitmap(blurredBitmap, 0, y, new Paint());
    mImageView.setImageBitmap(newBitmap);
}
 
Example 19
private void updateMediaSession(final String what) {
    int playState = mIsSupposedToBePlaying
            ? PlaybackStateCompat.STATE_PLAYING
            : PlaybackStateCompat.STATE_PAUSED;

    if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) {
        Bitmap albumArt = ImageLoader.getInstance().loadImageSync(TimberUtils.getAlbumArtUri(getAlbumId()).toString());
        if (albumArt != null) {

            Bitmap.Config config = albumArt.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }
            albumArt = albumArt.copy(config, false);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
                    .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName())
                    .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration())
                    .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1)
                    .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length)
                    .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName())
                    .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
                            mShowAlbumArtOnLockscreen ? albumArt : null)
                    .build());

            mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    }
}
 
Example 20
Source Project: myapplication   File: RoundImageView.java    License: Apache License 2.0 3 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }

    Bitmap b = ((BitmapDrawable) drawable).getBitmap();

    if (null == b) {
        return;
    }

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

    int w = getWidth(), h = getHeight();


    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    canvas.drawBitmap(roundBitmap, 0, 0, null);

}