在之前的博客中,关于通过Canvas配合Paint绘图保存在Bitmap上,最后展示在ImageView上,我还专门做了一个画图释义的Demo,具体请转至我用2D绘图API画了一只好丑的鸡,Canvas配合MotionEvent实现画板功能查看。具体就来介绍一下如何通过OpenCV4Android提供的API绘制这些基本形状并保存至Mat对象,然后转化为Bitmap在ImageView上显示出来。
1,绘图API
当前在Mat上绘图的实现在Imgproc模块中,那我们就来看看这个模块究竟有哪些关于绘制基本图形的接口。这里我想声明一下,因为其方法数量实在太多,这里不全都列举,只给出几个和今天介绍相关的接口方法;还有就是该类中的所有方法均为static方法。
- 画圆:
static void |
circle(Mat img, Point center, int radius, Scalar color) |
static void |
circle(Mat img, Point center, int radius, Scalar color, int thickness) |
static void |
circle(Mat img, Point center, int radius, Scalar color, int thickness, int lineType) |
static void |
circle(Mat img, Point center, int radius, Scalar color, int thickness, int lineType, int shift) |
其中:
img:表示接收绘制信息的Mat对象
center:圆心坐标(单位像素)
radius:半径(单位像素)
color:颜色对象
thickness:线条粗细
lintType:线条类型
shift:位置偏移
- 画椭圆
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color) |
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color, int thickness) |
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color, int thickness, int lineType) |
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color, int thickness, int lineType, int shift) |
static void |
ellipse(Mat img, RotatedRect box, Scalar color) |
static void |
ellipse(Mat img, RotatedRect box, Scalar color, int thickness) |
static void |
ellipse(Mat img, RotatedRect box, Scalar color, int thickness, int lineType) |
imag:接收绘制信息的Mat对象
center:椭圆圆心坐标
axes:长轴与短轴大小
angle:旋转角度
startAngle:开始角度
endAngle:结束角度
color:颜色对象
thickness:线条粗细
lineType:线条类型
shift:位置偏移
- 画线:
static void |
line(Mat img, Point pt1, Point pt2, Scalar color) |
static void |
line(Mat img, Point pt1, Point pt2, Scalar color, int thickness) |
static void |
line(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType) |
static void |
line(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType, int shift) |
imag:接收绘制信息的Mat对象
pt1:起始点坐标
pt2:终点坐标
color:颜色对象
thickness:线条粗细
lineType:线条类型
shift:位置偏移
- 画矩形:
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color) |
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness) |
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType) |
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType, int shift) |
imag:接收绘制信息的Mat对象
pt1:左上角坐标
pt2:右下角坐标
color:颜色对象
thickness:线条粗细
lineType:线条类型
shift:位置偏移
- 写文本:
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color) |
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color, int thickness) |
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType) |
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType, boolean bottomLeftOrigin) |
imag:接收绘制信息的Mat对象
text:文本内容
org:开始位置点坐标
fontFace:字体类型
fontScale:字体大小
color:颜色对象
thickness:线条粗细
lineType:线条类型
shift:位置偏移
2 示例
上面就是基本绘图API以及其参数介绍,其实调用就很贱单了,下面我们就来举一个示例说明一下使用的方法,还是一样,先看一下效果图:
activity_draw.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.operationopencvmain.Mat_Test.DrawActivity">
<LinearLayout
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="120px">
<ImageView
android:id="@+id/draw_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400px"
android:orientation="horizontal">
<ImageView
android:id="@+id/draw_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/draw_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400px"
android:orientation="horizontal">
<ImageView
android:id="@+id/draw_ellipse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/draw_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawRectangle"
android:text="矩形" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawCircle"
android:text="圆形" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawEllipse"
android:text="椭圆" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawLine"
android:text="直线" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawText"
android:text="文本" />
</LinearLayout>
</LinearLayout>
DrawActivity.java代码:
package com.hfut.operationopencvmain.Mat_Test;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.hfut.operationopencvmain.R;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
/**
* @author why
* @date 2018-11-28 10:04:45
*/
public class DrawActivity extends AppCompatActivity {
ImageView ranctangleImageView;
ImageView circleImageView;
ImageView ellipseImageView;
ImageView lineImageView;
ImageView textImageView;
private static final String TAG = "DrawActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
boolean status = OpenCVLoader.initDebug();
if (status) {
Log.e(TAG, "onCreate: Succese");
} else {
Log.e(TAG, "onCreate: Failed");
}
ranctangleImageView = findViewById(R.id.draw_rectangle);
circleImageView = findViewById(R.id.draw_circle);
ellipseImageView = findViewById(R.id.draw_ellipse);
lineImageView = findViewById(R.id.draw_line);
textImageView = findViewById(R.id.draw_text);
//imageView.setImageDrawable(getResources().getDrawable(R.drawable.test));
}
//绘制矩形
public void drawRectangle(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//绿色,宽度5个像素
Imgproc.rectangle(srcMat, new Point(100, 100), new Point(300, 300), new Scalar(0, 255, 0), 5);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
ranctangleImageView.setImageBitmap(destbitmap);
}
//绘制圆形
public void drawCircle(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//红色,宽度5个像素
Imgproc.circle(srcMat, new Point(200, 200), 120, new Scalar(0, 0, 255), 5);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
circleImageView.setImageBitmap(destbitmap);
}
//绘制椭圆
public void drawEllipse(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//红色,宽度5个像素
Imgproc.ellipse(srcMat, new Point(200, 200), new Size(100, 50),
360, 0, 360, new Scalar(255, 0, 0), 5, 8, 0);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
ellipseImageView.setImageBitmap(destbitmap);
}
//绘制直线
public void drawLine(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//蓝色,宽度5个像素
Imgproc.line(srcMat, new Point(100, 100), new Point(300, 300), new Scalar(255, 0, 0), 5);
Imgproc.line(srcMat, new Point(300, 100), new Point(100, 300), new Scalar(255, 0, 0), 5);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
lineImageView.setImageBitmap(destbitmap);
}
//绘制文本
public void drawText(View view) {
Mat srcMat = Mat.zeros(100, 600, CvType.CV_8UC3);
//蓝色,宽度5个像素
Imgproc.putText(srcMat, "Welcome to OpenCV4Android", new Point(50, 50),
Core.FONT_HERSHEY_PLAIN, 2, new Scalar(255, 0, 0), 5);
Bitmap destbitmap = Bitmap.createBitmap(600, 100, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
textImageView.setImageBitmap(destbitmap);
}
}
好了,这部分关于在Mat和Bitmap上绘图的知识就介绍完了,更多内容请参考:
下一篇: