由多边形区域裁剪图像 [英] Crop image by polygon area

查看:141
本文介绍了由多边形区域裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过多边形区域裁剪图像,但couldn`t发现任何库,它可以使。 OpenCV的太大,这个小东西。 JJIL [输入链接的描述在这里]作物刚刚矩形区域。 也许你有什么想法我如何能实现呢?感谢您的帮助!

有关尼迪: 尝试这样的事情,如果不列入工作 - 创建另一个画布路径,不是从它那里得到位图(掩模),并应用该掩码位图到您最初的画布上,而不是drawPath

 位图obmp = BitmapFactory.de codeResource(getResources(),R.drawable.image1);
位图resultImg = Bitmap.createBitmap(obmp.getWidth(),obmp.getHeight(),Bitmap.Config.ARGB_8888);
位图maskImg = Bitmap.createBitmap(obmp.getWidth(),obmp.getHeight(),Bitmap.Config.ARGB_8888);

帆布mCanvas =新的Canvas(resultImg);
帆布maskCanvas =新的Canvas(maskImg);

涂料粉刷=新的油漆(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias​​(真正的);
paint.setStyle(Paint.Style.FILL);;
paint.setXfermode(新PorterDuffXfermode(Mode.DST_IN));

路径path =新路径();
path.moveTo(view.mx,view.my);
path.lineTo(view.x1,view.y1);
path.lineTo(view.x2,view.y2);
path.lineTo(view.x3,view.y3);
path.lineTo(view.x4,view.y4);
path.close();

maskCanvas.drawPath(路径,油漆);
mCanvas.drawBitmap(obmp,0,0,NULL);
mCanvas.drawBitmap(maskImg,0,0,油漆);
 

解决方案

感谢Eddy_Em,我已经使用PorterDuffXfermode做到了这一点。 很好的例子

I want to crop image by polygon area, but couldn`t find any library, which can make it. OpenCV is too big for this small thing. JJIL [enter link description here] crop just rectangle area. Maybe you have any ideas how i can achieve it? Thanks for help!

FOR Nidhi: Try something like this, if doesnot work - create another canvas for path, and than get Bitmap from it (for mask), and apply this mask bitmap to your initial canvas instead drawPath.

Bitmap obmp = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
Bitmap resultImg = Bitmap.createBitmap(obmp.getWidth(), obmp.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap maskImg = Bitmap.createBitmap(obmp.getWidth(), obmp.getHeight(), Bitmap.Config.ARGB_8888);

Canvas mCanvas = new Canvas(resultImg);
Canvas maskCanvas = new Canvas(maskImg);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);;
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

Path path = new Path();
path.moveTo(view.mx,view.my);
path.lineTo(view.x1,view.y1);
path.lineTo(view.x2,view.y2 );
path.lineTo(view.x3,view.y3);
path.lineTo(view.x4,view.y4);
path.close();

maskCanvas.drawPath(path, paint);   
mCanvas.drawBitmap(obmp, 0, 0, null);
mCanvas.drawBitmap(maskImg, 0, 0, paint);

解决方案

Thanks for Eddy_Em, i have achieved this by using PorterDuffXfermode. Good example

这篇关于由多边形区域裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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