Java Code Examples for android.graphics.Bitmap.createBitmap()

The following are Jave code examples for showing how to use createBitmap() of the android.graphics.Bitmap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: FastBanner   File: IndicationView.java   View Source Code Vote up 6 votes
private Bitmap drawCircleBitmap(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(circleBitmap);

    Paint paint = new Paint();
    paint.setDither(true);
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);

    canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, getMeasuredWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, 0, 0, paint);

    bitmap.recycle();

    return circleBitmap;
}
 
Example 2
Project: mobile-store   File: QRCodeEncoder.java   View Source Code Vote up 6 votes
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example 3
Project: AndroidBackendlessChat   File: CircleImageView.java   View Source Code Vote up 6 votes
private Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    try {
        Bitmap bitmap;

        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (OutOfMemoryError e) {
        return null;
    }
}
 
Example 4
Project: musicplayer   File: BlurBitmap.java   View Source Code Vote up 6 votes
public Bitmap blur(Bitmap bitmap, int w,int h) {
    float scaleFactor = 1;
    /**
     * 这里是双重模糊手段
     * scaleFactor是将bitmap缩小至原来的 n 倍u,然后在填充的时候被拉伸 n 倍,从而模糊
     * radius是模糊半径,有FastBlur处理
     * 两者都是乘积越大,越模糊
     * */
    float radius = 20;
    if (bitmap != null) {
        scaleFactor = 18;
        radius = 8;
    }

    Log.e("test","w = " +w+ " h = "+ h / scaleFactor + "bitmap = "+bitmap);
    Bitmap overlay = Bitmap.createBitmap((int) (bitmap.getWidth() / scaleFactor),
            (int) (bitmap.getHeight() / scaleFactor), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.scale(1 / scaleFactor, 1 / scaleFactor);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);

    overlay = FastBlur.doBlur(overlay, (int) radius, true);
    return overlay;
}
 
Example 5
Project: wg_planer   File: CircularImageView.java   View Source Code Vote up 6 votes
private void initializeBitmap() {
    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;

    } else if (drawable instanceof BitmapDrawable) {
        mBitmap = ((BitmapDrawable) drawable).getBitmap();
        setup();

        return;

    } else if (drawable instanceof ColorDrawable) {
        mBitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);

    } else {
        mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                BITMAP_CONFIG);
    }

    Canvas canvas = new Canvas(mBitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    setup();
}
 
Example 6
Project: TPlayer   File: DrawableUtils.java   View Source Code Vote up 6 votes
public static Bitmap drawableToBitMap(Drawable drawable) {
    if (drawable == null) {
        return null;
    }
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
        return bitmapDrawable.getBitmap();
    } else {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}
 
Example 7
Project: DismissibleImageView   File: BlurTransformation.java   View Source Code Vote up 5 votes
@Override public Bitmap transform(Bitmap source) {

        int scaledWidth = source.getWidth() / mSampling;
        int scaledHeight = source.getHeight() / mSampling;

        Bitmap bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(source, 0, 0, paint);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            try {
                bitmap = RsBlur.blur(mContext, bitmap, mRadius);
            } catch (RSRuntimeException e) {
                bitmap = FastBlur.blur(bitmap, mRadius, true);
            }
        } else {
            bitmap = FastBlur.blur(bitmap, mRadius, true);
        }

        //source.recycle();

        return bitmap;
    }
 
Example 8
Project: Rxjava2.0Demo   File: DoActivity.java   View Source Code Vote up 5 votes
private Bitmap drawTBit(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}
 
Example 9
Project: DroidPlugin   File: IActivityManagerHookHandle.java   View Source Code Vote up 5 votes
private Bitmap drawableToBitMap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
        return bitmapDrawable.getBitmap();
    } else {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}
 
Example 10
Project: MontageCam   File: PhotoProcessing.java   View Source Code Vote up 5 votes
public static Bitmap rotate(Bitmap bitmap, int angle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Config config = bitmap.getConfig();
    nativeInitBitmap(width, height);
    sendBitmapToNative(bitmap);

    if (angle == 90) {
        nativeRotate90();
        bitmap.recycle();
        bitmap = Bitmap.createBitmap(height, width, config);
        bitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
    } else if (angle == 180) {
        nativeRotate180();
        bitmap.recycle();
        bitmap = Bitmap.createBitmap(width, height, config);
        bitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
    } else if (angle == 270) {
        nativeRotate180();
        nativeRotate90();
        bitmap.recycle();
        bitmap = Bitmap.createBitmap(height, width, config);
        bitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
    }
    return bitmap;
}
 
Example 11
Project: Open-Quran-Radio   File: ImageHelper.java   View Source Code Vote up 5 votes
private Bitmap composeImages(Bitmap background, int size) {

        // compose output image
        Bitmap outputImage = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outputImage);
        canvas.drawBitmap(background, 0, 0, null);
        canvas.drawBitmap(mInputImage, createTransformationMatrix(size), null);

        return outputImage;
    }
 
Example 12
Project: mapbox-navigation-android   File: Utils.java   View Source Code Vote up 5 votes
/**
 * Demonstrates converting any Drawable to an Icon, for use as a marker icon.
 */
public static Icon drawableToIcon(@NonNull Context context, @DrawableRes int id) {
  Drawable vectorDrawable = ResourcesCompat.getDrawable(context.getResources(), id, context.getTheme());
  Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
    vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  vectorDrawable.draw(canvas);
  return IconFactory.getInstance(context).fromBitmap(bitmap);
}
 
Example 13
Project: eggs-android   File: RoundedImageView.java   View Source Code Vote up 5 votes
@Override
protected void onDraw(Canvas canvas) {

    try {
        Drawable drawable = getDrawable();

        if (drawable == null) {
            return;
        }

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

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

        if (w <= 0 || h <= 0) {
            return;
        }

        if (drawable instanceof BitmapDrawable) {
            Bitmap b = ((BitmapDrawable) drawable).getBitmap();
            bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        } else if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
            Canvas c = new Canvas(bitmap);
            c.drawColor(((ColorDrawable) drawable).getColor());

        } else {
            return;
        }

        Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, Math.min(w, h));
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    } catch (Exception e) {
        Log.e(TAG, "onDraw Exception", e);
    }

}
 
Example 14
Project: ImageEraser   File: MainActivity.java   View Source Code Vote up 5 votes
private void makeHighResolutionOutput() {
    if (this.isImageResized) {
        Bitmap solidColor = Bitmap.createBitmap(this.originalBitmap.getWidth(), this.originalBitmap.getHeight(), this.originalBitmap.getConfig());
        Canvas canvas = new Canvas(solidColor);
        Paint paint = new Paint();
        paint.setColor(Color.argb(255, 255, 255, 255));
        Rect src = new Rect(0, 0, this.bitmapMaster.getWidth(), this.bitmapMaster.getHeight());
        Rect dest = new Rect(0, 0, this.originalBitmap.getWidth(), this.originalBitmap.getHeight());
        canvas.drawRect(dest, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
        canvas.drawBitmap(this.bitmapMaster, src, dest, paint);
        this.highResolutionOutput = null;
        this.highResolutionOutput = Bitmap.createBitmap(this.originalBitmap.getWidth(), this.originalBitmap.getHeight(), this.originalBitmap.getConfig());
        Canvas canvas1 = new Canvas(this.highResolutionOutput);
        canvas1.drawBitmap(this.originalBitmap, 0.0f, 0.0f, null);
        Paint paint1 = new Paint();
        paint1.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
        canvas1.drawBitmap(solidColor, 0.0f, 0.0f, paint1);
        if (solidColor != null && !solidColor.isRecycled()) {
            solidColor.recycle();
            solidColor = null;
        }
        return;
    }
    this.highResolutionOutput = null;
    this.highResolutionOutput = this.bitmapMaster.copy(this.bitmapMaster.getConfig(), true);
}
 
Example 15
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 5 votes
@Test
public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  toTransform.setHasAlpha(true);

  Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform,
      toTransform.getWidth() / 2, toTransform.getHeight() / 2);

  assertTrue(result.hasAlpha());
}
 
Example 16
Project: WithYou   File: VideoVerify.java   View Source Code Vote up 5 votes
private Bitmap RotateDeg90(Bitmap bmp)
{
	// ����������  
	Matrix matrix = new Matrix();  
    // ����ԭͼ  
    matrix.postScale(1f, 1f);  
    // ������ת45�ȣ�����Ϊ����������ת  
    matrix.postRotate(-90);  
    //bmp.getWidth(), 500�ֱ��ʾ�ػ���λͼ���  
    Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),  
            matrix, true);
	return dstbmp;  
}
 
Example 17
Project: RLibrary   File: BmpUtil.java   View Source Code Vote up 4 votes
/**
 * Clip circle bitmap bitmap.
 *
 * @param bitmap the bitmap
 * @return the bitmap
 */
public static Bitmap clipCircleBitmap(Bitmap bitmap) {
    if (bitmap == null || bitmap.isRecycled())
        return null;

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top = 0, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    width = width - 2;
    height = height - 2;
    if (width <= height) {
        roundPx = width / 2;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

    Bitmap output = Bitmap.createBitmap(width,
            height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);

    paint.setAntiAlias(true);


    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);

    if (!bitmap.isRecycled()) {
        bitmap.recycle();
    }

    return output;
}
 
Example 18
Project: rongyunDemo   File: BitmapUtils.java   View Source Code Vote up 4 votes
/**
 * 转换图片成圆形
 *
 * @param bitmap
 *            传入Bitmap对象
 * @return
 */
public static Bitmap toRoundBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2;
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }
    Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right,
                              (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top,
                              (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);

    if (null != bitmap) {
        bitmap.recycle();
        bitmap = null;
    }
    return output;
}
 
Example 19
Project: musicwidget   File: NotificationListener.java   View Source Code Vote up 4 votes
public void updateMetadata(){
    if(mediaController!=null&&mediaController.getPlaybackState()!=null){
        StandardWidget.currentlyPlaying = mediaController.getPlaybackState().getState() == PlaybackState.STATE_PLAYING;
    }
    if(meta==null)return;
    StandardWidget.currentArt=meta.getBitmap(MediaMetadata.METADATA_KEY_ART);
    if(StandardWidget.currentArt==null){
        StandardWidget.currentArt = meta.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
    }
    if(StandardWidget.currentArt==null){
        StandardWidget.currentArt = meta.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON);
    }
    StandardWidget.currentSquareArt = StandardWidget.currentArt;
    if(StandardWidget.currentArt!=null) {
        int cah = StandardWidget.currentArt.getHeight();
        int caw = StandardWidget.currentArt.getWidth();
        if (caw > cah * 1.02) {
            StandardWidget.currentSquareArt = Bitmap.createBitmap(StandardWidget.currentArt,
                    (int) (caw / 2 - cah * 0.51), 0, (int) (cah * 1.02), cah);
        }
    }
    StandardWidget.currentArtist=meta.getString(MediaMetadata.METADATA_KEY_ARTIST);
    StandardWidget.currentSong=meta.getString(MediaMetadata.METADATA_KEY_TITLE);
    if(StandardWidget.currentSong==null){
        StandardWidget.currentSong=meta.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE);
    }
    StandardWidget.currentAlbum=meta.getString(MediaMetadata.METADATA_KEY_ALBUM);
    if(StandardWidget.currentArtist==null){
        StandardWidget.currentArtist = meta.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST);
    }
    if(StandardWidget.currentArtist==null) {
        StandardWidget.currentArtist = meta.getString(MediaMetadata.METADATA_KEY_AUTHOR);
    }
    if(StandardWidget.currentArtist==null) {
        StandardWidget.currentArtist = meta.getString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE);
    }
    if(StandardWidget.currentArtist==null) {
        StandardWidget.currentArtist = meta.getString(MediaMetadata.METADATA_KEY_WRITER);
    }
    if(StandardWidget.currentArtist==null) {
        StandardWidget.currentArtist = meta.getString(MediaMetadata.METADATA_KEY_COMPOSER);
    }
    if(StandardWidget.currentArtist==null)StandardWidget.currentArtist = "";
    if(StandardWidget.currentSong==null)StandardWidget.currentSong = "";
    if(StandardWidget.currentAlbum==null)StandardWidget.currentAlbum = "";
    sendBroadcast(new Intent(StandardWidget.WIDGET_UPDATE));
}
 
Example 20
Project: Checkerboard-IMU-Comparator   File: CameraBridgeViewBase.java   View Source Code Vote up 4 votes
protected void AllocateCache()
{
    mCacheBitmap = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
}