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

The following examples show how to use android.graphics.Bitmap#createBitmap() . 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 check out the related API usage on the sidebar.
Example 1
Source File: ScanShapeView.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 计算动画相关信息
 * @param canvas 画布
 */
private void makeAnim(Canvas canvas) {
    // 判断形状类型
    switch (mShapeType) {
        case Square: // 正方形
            // 正方形不需要绘制计算 ( 初始化 )
            break;
        case Hexagon: // 六边形
            // 获取绘制的区域 ( 绘制扫描区域 + 线条居于绘制边框距离 )
            RectF rectF = calcShapeRegion(mLineMarginToHexagon);
            // 线条路径计算重置起始位置
            RectF lineRectF = new RectF(0, 0, rectF.right - rectF.left, rectF.right - rectF.left);
            // 计算边距处理
            mLinePathToHexagon = makeShape(lineRectF, canvas, mLinePaintToHexagon, false);
            // 生成 Bitmap
            mBitmapToHexagon = Bitmap.createBitmap((int) (rectF.right - rectF.left), (int) (rectF.right - rectF.left), Bitmap.Config.ARGB_8888);
            // 生成新的 Canvas
            mCanvasToHexagon = new Canvas(mBitmapToHexagon);
            // 计算中心点
            mCenterToHexagon = ((rectF.right - rectF.left) / 2);
            break;
        case Annulus: // 环形
            // 环形不需要绘制计算 ( 初始化 )
            break;
    }
}
 
Example 2
Source File: ViewUtils.java    From Jockey with Apache License 2.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(
            drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);

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

    return bitmap;
}
 
Example 3
Source File: ImageUtils.java    From ImageFileSelector with Apache License 2.0 6 votes vote down vote up
private static Bitmap rotateImageNotNull(int angle, Bitmap bitmap) {
    Bitmap out = null;
    try {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        out = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (OutOfMemoryError e) {
        AppLogger.printStackTrace(e);
        AppLogger.e(ImageCompressHelper.TAG, "rotate image error, image will not display in current orientation");
    }
    if (out != null) {
        bitmap.recycle();
        return out;
    }
    return bitmap;
}
 
Example 4
Source File: ImageDiscernActivity.java    From VMLibrary with Apache License 2.0 6 votes vote down vote up
@Override
    protected void init() {
        Bitmap oneBitmap = BitmapFactory.decodeFile("/sdcard/DCIM/Screenshots/verification_code_1.png");
        Bitmap twoBitmap = BitmapFactory.decodeFile("/sdcard/DCIM/Screenshots/verification_code_2.png");
//        Bitmap threeBitmap = BitmapFactory.decodeFile("/sdcard/DCIM/Screenshots/verification_code_2.png");
        if (oneBitmap == null || twoBitmap == null) {
            return;
        }
        // 剪切一下图片
        Rect mRect = new Rect(120, 800, 950, 1300);
        oneBitmap = Bitmap.createBitmap(oneBitmap, mRect.left, mRect.top, mRect.right - mRect.left, mRect.bottom - mRect.top);
        twoBitmap = Bitmap.createBitmap(twoBitmap, mRect.left, mRect.top, mRect.right - mRect.left, mRect.bottom - mRect.top);

        Point startPoint = new Point(0, 0);
        Point targetPoint = new Point(0, 0);

        Bitmap destBitmap = ImageDsicern.compareBitmap(oneBitmap, twoBitmap, startPoint, targetPoint);

        oneView.setImageBitmap(oneBitmap);
        twoView.setImageBitmap(twoBitmap);
        destView.setImageBitmap(destBitmap);

        VMLog.d("不同点坐标: start(%d, %d) - target(%d, %d)", startPoint.x, startPoint.y, targetPoint.x, targetPoint.y);
    }
 
Example 5
Source File: UI.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap getQRCode(final String data) {
    final ByteMatrix matrix;
    try {
        matrix = Encoder.encode(data, ErrorCorrectionLevel.M).getMatrix();
    } catch (final WriterException e) {
        throw new RuntimeException(e);
    }

    final int height = matrix.getHeight() * SCALE;
    final int width = matrix.getWidth() * SCALE;
    final int min = height < width ? height : width;

    final Bitmap mQRCode = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
    for (int x = 0; x < min; ++x)
        for (int y = 0; y < min; ++y)
            mQRCode.setPixel(x, y, matrix.get(x / SCALE, y / SCALE) == 1 ? Color.BLACK : Color.TRANSPARENT);
    return mQRCode;
}
 
Example 6
Source File: ConvertToBitmapUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 将ScrollView转换为Bitmap
 * @param scrollView
 * @return
 */
public static Bitmap convertScrollViewToBitmap(ScrollView scrollView) {
    int h = 0;
    Bitmap bitmap = null;
    // 获取scrollview实际高度
    for (int i = 0; i < scrollView.getChildCount(); i++) {
        h += scrollView.getChildAt(i).getHeight();
        scrollView.getChildAt(i).setBackgroundColor(
                Color.parseColor("#ffffff"));
    }
    // 创建对应大小的bitmap
    bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
            Bitmap.Config.RGB_565);
    final Canvas canvas = new Canvas(bitmap);
    scrollView.draw(canvas);
    return bitmap;
}
 
Example 7
Source File: RoundedThumbnailView.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains a square bitmap by cropping the center of a bitmap. If the given image is
 * portrait, the cropped image keeps 100% original width and vertically centered to the
 * original image. If the given image is landscape, the cropped image keeps 100% original
 * height and horizontally centered to the original image.
 *
 * @param srcBitmap the bitmap image to be cropped in the center.
 * @return a result square bitmap.
 */
private Bitmap cropCenterBitmap(Bitmap srcBitmap)
{
    int srcWidth = srcBitmap.getWidth();
    int srcHeight = srcBitmap.getHeight();
    Bitmap dstBitmap;
    if (srcWidth >= srcHeight)
    {
        dstBitmap = Bitmap.createBitmap(
                srcBitmap, srcWidth / 2 - srcHeight / 2, 0, srcHeight, srcHeight);
    } else
    {
        dstBitmap = Bitmap.createBitmap(
                srcBitmap, 0, srcHeight / 2 - srcWidth / 2, srcWidth, srcWidth);
    }
    return dstBitmap;
}
 
Example 8
Source File: CircleTransform.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;

    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    // TODO this could be acquired from the pool too
    Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

    Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    return result;
}
 
Example 9
Source File: CanvasView.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This method updates the instance of Canvas (View)
 *
 * @param canvas the new instance of Canvas
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Before "drawPath"
    canvas.drawColor(this.baseColor);

    if (this.bitmap != null) {
        Matrix m = new Matrix();
        m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), Matrix.ScaleToFit.CENTER);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
        height = (((canvas.getHeight()/2)-bitmap.getHeight()/2));
        width=((canvas.getWidth()/2)-bitmap.getWidth()/2);
        canvas.drawBitmap(bitmap,width,height, new Paint());
        right = canvas.getWidth();
        bottom = height + ((bitmap.getHeight()));
        canvas.clipRect(0,height,right, bottom);
    }

    for (int i = 0; i < this.historyPointer; i++) {
        Path path = this.pathLists.get(i);
        Paint paint = this.paintLists.get(i);

        canvas.drawPath(path, paint);
    }

    this.drawText(canvas);
    this.canvas = canvas;

}
 
Example 10
Source File: ImageUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
public static Bitmap view2Bitmap(final View view) {
    if (view == null) return null;
    Bitmap ret = Bitmap.createBitmap(view.getWidth(),
            view.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(ret);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null) {
        bgDrawable.draw(canvas);
    } else {
        canvas.drawColor(Color.WHITE);
    }
    view.draw(canvas);
    return ret;
}
 
Example 11
Source File: BitmapUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 处理图片
 *
 * @param bitmapOrg 所要转换的bitmap
 * @param newWidth  新的宽
 * @param newHeight 新的高
 * @return 指定宽高的bitmap
 */
public static Bitmap zoom(Bitmap bitmapOrg, int newWidth, int newHeight) {

    if (bitmapOrg.getWidth() < newWidth
            || bitmapOrg.getHeight() < newHeight) {
        if (bitmapOrg.getWidth() < newWidth) {
            newHeight = newWidth = bitmapOrg.getWidth() / 2;
        }

        if (newHeight < bitmapOrg.getHeight()) {
            newHeight = newWidth = bitmapOrg.getHeight() / 2;
        }
    }
    int width = bitmapOrg.getWidth();
    int height = bitmapOrg.getHeight();

    // 计算缩放率,新尺寸除原始尺寸
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // 创建操作图片用的matrix对象
    Matrix matrix = new Matrix();

    // 缩放图片动作
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
            height, matrix, true);

    return resizedBitmap;
}
 
Example 12
Source File: ScreenGrabber.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
private static Bitmap grab(final int pGrabX, final int pGrabY, final int pGrabWidth, final int pGrabHeight) {
	final int[] source = new int[pGrabWidth * (pGrabY + pGrabHeight)];
	final IntBuffer sourceBuffer = IntBuffer.wrap(source);
	sourceBuffer.position(0);

	// TODO Check availability of OpenGL and GLES20.GL_RGBA combinations that require less conversion operations.
	// Note: There is (said to be) a bug with glReadPixels when 'y != 0', so we simply read starting from 'y == 0'.
	// TODO Does that bug still exist?
	GLES20.glReadPixels(pGrabX, 0, pGrabWidth, pGrabY + pGrabHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, sourceBuffer);

	final int[] pixels = new int[pGrabWidth * pGrabHeight];

	// Convert from RGBA_8888 (Which is actually ABGR as the whole buffer seems to be inverted) --> ARGB_8888
	for (int y = 0; y < pGrabHeight; y++) {
		for (int x = 0; x < pGrabWidth; x++) {
			final int pixel = source[x + ((pGrabY + y) * pGrabWidth)];

			final int blue = (pixel & 0x00FF0000) >> 16;
			final int red = (pixel  & 0x000000FF) << 16;
			final int greenAlpha = pixel & 0xFF00FF00;

			pixels[x + ((pGrabHeight - y - 1) * pGrabWidth)] = greenAlpha | red | blue;
		}
	}

	return Bitmap.createBitmap(pixels, pGrabWidth, pGrabHeight, Config.ARGB_8888);
}
 
Example 13
Source File: BadgedFourThreeImageView.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
GifBadge(Context context) {
    if (bitmap == null) {
        final DisplayMetrics dm = context.getResources().getDisplayMetrics();
        final float density = dm.density;
        final float scaledDensity = dm.scaledDensity;
        final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint
                .SUBPIXEL_TEXT_FLAG);
        textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE));
        textPaint.setTextSize(TEXT_SIZE * scaledDensity);

        final float padding = PADDING * density;
        final float cornerRadius = CORNER_RADIUS * density;
        final Rect textBounds = new Rect();
        textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds);
        height = (int) (padding + textBounds.height() + padding);
        width = (int) (padding + textBounds.width() + padding);
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setHasAlpha(true);
        final Canvas canvas = new Canvas(bitmap);
        final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(BACKGROUND_COLOR);
        canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius,
                backgroundPaint);
        // punch out the word 'GIF', leaving transparency
        textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawText(GIF, padding, height - padding, textPaint);
    }
    paint = new Paint();
}
 
Example 14
Source File: BucketsBitmapPool.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Allocate a bitmap that has a backing memory allocation of 'size' bytes. This is configuration
 * agnostic so the size is the actual size in bytes of the bitmap.
 *
 * @param size the 'size' in bytes of the bitmap
 * @return a new bitmap with the specified size in memory
 */
@Override
protected Bitmap alloc(int size) {
  return Bitmap.createBitmap(
      1,
      (int) Math.ceil(size / (double) BitmapUtil.RGB_565_BYTES_PER_PIXEL),
      Bitmap.Config.RGB_565);
}
 
Example 15
Source File: BitmapRotateTransform.java    From Mrthumb with Apache License 2.0 5 votes vote down vote up
public static Bitmap transform(Bitmap bitmap, float rotate) {
    Matrix matrix = new Matrix();
    matrix.postRotate(rotate);
    int frameWidth = bitmap.getWidth();
    int frameHeight = bitmap.getHeight();
    return Bitmap.createBitmap(bitmap, 0, 0, frameWidth, frameHeight, matrix, true);
}
 
Example 16
Source File: BookPageFactory.java    From eBook with Apache License 2.0 5 votes vote down vote up
public Bitmap drawPrePage(float powerPercent) {
    if (mReadInfo.isLastNext) {
        pageUp();

        mReadInfo.isLastNext = false;
    }

    Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(mPaintInfo.bgColor);

    //下一页
    mPageLines = getPrePageLines();
    //已经到第一页了
    if (mPageLines.size() == 0 || mPageLines == null) {
        return null;
    }

    float y = mPaintInfo.textSize;

    for (String strLine : mPageLines) {
        y += mLineHeight;
        canvas.drawText(strLine, marginWidth, y, mPaint);
    }

    //绘制显示的信息
    drawInfo(canvas, powerPercent);

    return bitmap;

}
 
Example 17
Source File: ImageLoadUtil.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
/**
 * @param drawable
 * @return
 */
public Bitmap drawable2Bitmap(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 18
Source File: FloatActionButton.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public void setFloatingActionButtonDrawable(Drawable FloatingActionButtonDrawable) {
//        mBitmap = ((BitmapDrawable) FloatingActionButtonDrawable).getBitmap();


        if(FloatingActionButtonDrawable instanceof BitmapDrawable) {
            mBitmap  = ((BitmapDrawable)FloatingActionButtonDrawable).getBitmap();
        }else{
            Bitmap bitmap = Bitmap.createBitmap(FloatingActionButtonDrawable.getIntrinsicWidth(),FloatingActionButtonDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            FloatingActionButtonDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            FloatingActionButtonDrawable.draw(canvas);
            mBitmap = bitmap;
        }
        invalidate();
    }
 
Example 19
Source File: AvatarBitmapTransformation.java    From barterli_android with Apache License 2.0 4 votes vote down vote up
@Override
public Bitmap transform(final Bitmap source) {

    final boolean alreadySquare = isSquareImage(source.getWidth(), source.getHeight());

    final int avatarSizeInPixels = BarterLiApplication
            .getStaticContext()
            .getResources().getDimensionPixelSize(mAvatarSize.dimenResId);
    final int shortestWidth = Math.min(source.getWidth(), source.getHeight());
    final float scaleFactor = avatarSizeInPixels / (float) shortestWidth;

    final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
            source,
            (int) (source.getWidth() * scaleFactor),
            (int) (source.getHeight() * scaleFactor),
            true);

    if (scaledBitmap != source) {
        source.recycle();
    }

    if (alreadySquare) {
        //Already square, no need to crop
        return scaledBitmap;
    } else {
        final int size = Math.min(scaledBitmap.getWidth(), scaledBitmap.getHeight());

        //Start x,y positions to crop the bitmap
        int x = 0;
        int y = 0;

        if (size == scaledBitmap.getWidth()) {
        /* Portrait picture, crop the bottom and top parts */
            y = (scaledBitmap.getHeight() - size) / 2;

        } else {

        /* Landscape picture, crop the right and left zones */
            x = (scaledBitmap.getWidth() - size) / 2;
        }
        final Bitmap result = Bitmap.createBitmap(scaledBitmap, x, y, size, size);
        if (result != scaledBitmap) {
            scaledBitmap.recycle();
        }
        return result;
    }
}
 
Example 20
Source File: ClassifierActivity.java    From android-yolo-v2 with Do What The F*ck You Want To Public License 4 votes vote down vote up
private void fillCroppedBitmap(final Image image) {
        Bitmap rgbFrameBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
        rgbFrameBitmap.setPixels(ImageUtils.convertYUVToARGB(image, previewWidth, previewHeight),
                0, previewWidth, 0, 0, previewWidth, previewHeight);
        new Canvas(croppedBitmap).drawBitmap(rgbFrameBitmap, frameToCropTransform, null);
}