【问题标题】:Draw outline from image从图像中绘制轮廓
【发布时间】:2020-04-25 01:16:36
【问题描述】:

我想通过在背景中绘制并扩展第二张图片来在图片周围绘制轮廓,但我不太成功,如何绘制常规笔触?

我画的轮廓:

我要画的轮廓:

我的代码;

private Bitmap ContourBitmap() {
int strokeWidth = 8;
Bitmap originalBitmap =  BitmapFactory.decodeResource(getResources(), R.drawable.flower_icon);
Bitmap newStrokedBitmap = Bitmap.createBitmap(originalBitmap.getWidth() + 2 * strokeWidth, 
originalBitmap.getHeight() + 2 * strokeWidth, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newStrokedBitmap);
float scaleX = (originalBitmap.getWidth() + 2.0f * strokeWidth) / originalBitmap.getWidth();
float scaleY = (originalBitmap.getHeight() + 2.0f * strokeWidth) / originalBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY);
canvas.drawBitmap(originalBitmap, matrix, null);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_ATOP); //Color.WHITE is stroke color
canvas.drawBitmap(originalBitmap, strokeWidth, strokeWidth, null);
}

【问题讨论】:

    标签: android matrix canvas bitmap contour


    【解决方案1】:

    我认为你在正确的轨道上......

    这里是一个策略,而不是缩放只是绘制相同的原始图片几次,每次稍微偏移,请参见下面的示例:

    var canvas = document.getElementById('canvas')
    var ctx = canvas.getContext('2d')
    var img = new Image;
    img.onload = draw;
    img.src = "http://i.stack.imgur.com/UFBxY.png";
    
    var s = 10, // thickness scale
      x = 15, // final position
      y = 15;
    
    function draw() {
      ctx.globalAlpha = 0.2
      ctx.filter = 'brightness(0%)'
      for (i = 0; i < 360; i++)
        ctx.drawImage(img, x + Math.sin(i) * s, y + Math.cos(i) * s);
      ctx.globalAlpha = 1
      ctx.filter = 'none'
      ctx.drawImage(img, x, y);
    }
    &lt;canvas id=canvas width=350 height=600&gt;&lt;/canvas&gt;

    我申请了filter = 'brightness(0%)',但您可以申请更多:
    https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter

    我正在使用 HTML 画布,但同样的想法应该可以很好地“转化”为 android 画布。

    【讨论】:

    • 我无法将html语言翻译成Android语言,还是非常感谢。
    • 谢谢我试试答案,我想会成功
    猜你喜欢
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 2019-01-24
    相关资源
    最近更新 更多