如何在OpenCV Camera Preview上画线?

时间:2014-02-03 15:16:47

标签: android opencv camera overlay preview

我正在使用OpenCV相机预览。

我想在实时预览上绘制一个矩形。 我试图覆盖ondraw方法,但我看不到一条线。 请帮忙。

package com.example.easymeasure;

import org.opencv.android.JavaCameraView;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

public class CustomOpenCVJavaCameraView extends JavaCameraView {

    public CustomOpenCVJavaCameraView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private Paint linePaint;
    protected void init() { 
        Resources r = this.getResources();
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setAlpha(200);
        linePaint.setStrokeWidth(1);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setColor(r.getColor(R.color.marker_color));
        linePaint.setShadowLayer(2, 1, 1, r.getColor(R.color.shadow_color));        
    }

    @Override
      protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);       
        linePaint.setStrokeWidth(5);
        canvas.drawLine(10,20,1000,200,linePaint);
    }
}

2 个答案:

答案 0 :(得分:2)

见下解决方案:Extended SurfaceView's onDraw() method never called

您只需添加:

setWillNotDraw(false)

到构造函数

答案 1 :(得分:0)

我想补充的另一点: 覆盖onDraw方法后,将布局中的视图更改为:

CameraActivity