如何在圆柱形状中制作任何位图,就像添加到Android中任何杯子的图像一样

时间:2017-01-20 07:26:04

标签: android bitmap

`enter image description here

我的项目中有一项要求是将任何位图设置到马克杯中,并编辑位图并在图像上调整一些文本,然后通过此位图将其装满就像附加图像一样。

我需要在Android或任何受支持的库中使用解决方案,在我尝试使用Android Canvas及其方法绘制弧之前,但未达到我的要求。

/**
 * Draw one image over other using the canvas paint and path drawing.
 */
public Bitmap createMaskedImageInImageCenterRightMug(Drawable back,
                                                     Bitmap bitmapToDrawInTheCenter) {

    Bitmap backgroundBitmap = ((BitmapDrawable) back).getBitmap();
    int hieghtBack = backgroundBitmap.getHeight();
    int widthBack = backgroundBitmap.getWidth();
    int hieghtFront = bitmapToDrawInTheCenter.getHeight();
    int widthFront = bitmapToDrawInTheCenter.getWidth();
    int widthToDrawOnMug = widthBack / 2;
    backgroundBitmap = Bitmap.createScaledBitmap(backgroundBitmap, (int) canvas_width, hieghtBack, true);
    // Create mask
    Bitmap backgroundBitmapMask = Bitmap.createBitmap(backgroundBitmap, 7, 0, (int) (canvas_width / 2), hieghtBack);
    if (widthToDrawOnMug <= widthFront) {
        bitmapToDrawInTheCenter = Bitmap.createBitmap(
                bitmapToDrawInTheCenter, (widthFront * 40) / 100, 0,
                (widthFront * 60) / 100, hieghtFront);
    }
    Bitmap backgroundBitmapScaledMask = Bitmap.createScaledBitmap(
            backgroundBitmapMask, widthToDrawOnMug, hieghtBack - 50, true);

    bitmapToDrawInTheCenter = Bitmap.createScaledBitmap(
            bitmapToDrawInTheCenter, backgroundBitmapScaledMask.getWidth(),
            backgroundBitmapScaledMask.getHeight(), true);
    Bitmap resultBitmap = Bitmap.createBitmap(
            backgroundBitmapScaledMask.getWidth(),
            backgroundBitmapScaledMask.getHeight(),
            backgroundBitmapScaledMask.getConfig());
    Canvas canvas = new Canvas(resultBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(backgroundBitmapScaledMask, 0, 0, null);
    canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 0, paint);
    return resultBitmap;
}

/**
 * Draw one image over other using the canvas paint and path drawing.
 */
public Bitmap pasteOverMugForRightMug(Drawable back,
                                      Bitmap bitmapToDrawInTheCenter) {
    Bitmap backgroundBitmap = ((BitmapDrawable) back).getBitmap();
    Bitmap resultBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(backgroundBitmap, new Matrix(), null);
    Paint paint = new Paint();
    canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 25, paint);
    return resultBitmap;
}

这是我的代码。根据普通Mug图像宽高比使用一些硬代码值。 也尝试用Open GL做到这一点,但它非常复杂。 并且在图像处理库Image magic中找不到圆柱形状的方法。

如果有任何想法请分享。

0 个答案:

没有答案