从位图中切割多点多边形并将其放置在透明上 [英] Cutting a multipoint ploygon out of Bitmap and placing it on transparency

查看:15
本文介绍了从位图中切割多点多边形并将其放置在透明上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位图,我正在从中切出一个多点多边形.我很好奇获取任意形状内的像素并将它们复制到新位图上的正确过程是什么,其余像素是透明的.目标是允许用户追踪形状,然后移除多边形外的所有内容.

I have a bitmap out of which I'm cutting out a multipi point polygon. I'm curious what the correct process is for taking the pixels within the arbitrary shape and copying them onto a new bitmap where the rest of the pixels are transparent. The objective is to allow the user to trace the shape and then remove everything outside the polygon.

我已经计算出多边形部分(作为一个点数组),但现在我不知道如何将选定的像素转移到新的位图.

I have the polygon part worked out (as a an array of points), but now am stumped as to how to transfer just the selected pixels to a new Bitmap.

TIA

推荐答案

不确定你的代码是如何工作的,但这里有一个关于如何去做的想法:

Not sure how your code works, but here's an idea on how to do it:

  1. 计算选定区域的边界矩形(从您的点中找到最小 x、最小 y、最大 x 和最大 y).
  2. 使用任何 BitmapCanvas 方法将图像裁剪到边界矩形.
  3. 创建一个 Path 从你的点,全部移动到您的新位图 (x-=minX, y-=minY);
  4. 设置您的路径 FillType 到相反的一个(填充外部).
  5. 在新裁剪的画布上,使用 Xfermode 的颜料绘制路径为 PorterDuff.CLEAR,删除所有颜色.
  1. Calculate the bounding rectangle of the selected area (find min x, min y, max x and max y from your points).
  2. Crop your image to the bounding rectangle using any of the Bitmap or Canvas-methods.
  3. Create a Path from your points, all moved into your new bitmap (x-=minX, y-=minY);
  4. Set your Paths FillType to one that is inverse (fill the outside).
  5. On your new cropped canvas, draw the Path using a paint with the Xfermode as PorterDuff.CLEAR, which removes all color.

这篇关于从位图中切割多点多边形并将其放置在透明上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆