OpenCV的实施与自定义布局(上SurfaceView) [英] opencv implementation with with custom layout (on SurfaceView)

查看:635
本文介绍了OpenCV的实施与自定义布局(上SurfaceView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OpenCV的应用程序的工作,但需要按钮等添加到布局。所以基本上我想在surfaceView显示OpenCV的摄像头视图和下方添加其他的东西。

I have an openCV application program working, but need to add buttons etc. to the layout. So basically I want to display the opencv camera view on a surfaceView and the add the other stuff underneath.

我一直在寻找互联网和论坛了一会儿,才看到了OpenCV的人脸检测中的应用也想添加自定义布局......无解的人。

I've been searching the internet and forums for a while, only seeing the guy with a opencv facial detection application also wanting to add a custom layout... no solution.

我是一个解决方案,因此会非常AP preciate帮助真的绝望了。为此,我使用了OpenCV的样品3应用程序(如一个简单的例子),并试图绑定到surfaceview一个简单的自定义布局。我设法在一个正常的摄像头应用程序,但挣扎颇有几分与OpenCV的例子。

I am really desperate for a solution so would hugely appreciate help. For this purpose I used the OpenCV sample 3 application (as a simple example) and tried to bind to a surfaceview on a simple custom layout. I managed it in a normal Camera application, but struggling quite a bit with the opencv example.

因此​​,这是code,我有对Sample3Native.java,Sample3View.java和SampleViewBase.java(如实例)文件分别是:

So this is the code that I have for the Sample3Native.java, Sample3View.java and SampleViewBase.java (as in example) files respectively:

public class Sample3Native extends Activity {
private Sample3View mView;

private BaseLoaderCallback  mOpenCVCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {

                // Load native library after(!) OpenCV initialization
                System.loadLibrary("native_sample");

                // Create and set View
                mView = new Sample3View(mAppContext);
                setContentView(R.layout.main);
                //setContentView(mView);

                // Check native OpenCV camera
                mView.openCamera(); 
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

//constructor
public Sample3Native() {}

@Override
protected void onPause() {
    super.onPause();
    if (null != mView)
        mView.releaseCamera();
}

@Override
protected void onResume() {
    super.onResume();
    if((null != mView) && !mView.openCamera() ) {
        AlertDialog ad = new AlertDialog.Builder(this).create();  
        ad.setCancelable(false); // This blocks the 'BACK' button  
        ad.setMessage("Fatal error: can't open camera!");  
        ad.setButton("OK", new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int which) {  
            dialog.dismiss();
            finish();
            }  
        });  
        ad.show();
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack);
}

}

class Sample3View extends SampleViewBase {

private int mFrameSize;
private Bitmap mBitmap;
private int[] mRGBA;

public Sample3View(Context context) {
    super(context);
}

@Override
protected void onPreviewStarted(int previewWidtd, int previewHeight) {
    mFrameSize = previewWidtd * previewHeight;
    mRGBA = new int[mFrameSize];
    mBitmap = Bitmap.createBitmap(previewWidtd, previewHeight, Bitmap.Config.ARGB_8888);
}

@Override
protected void onPreviewStopped() {
    if(mBitmap != null) {
        mBitmap.recycle();
        mBitmap = null;
    }
    mRGBA = null;   
}

@Override
protected Bitmap processFrame(byte[] data) {
    int[] rgba = mRGBA;

    FindFeatures(getFrameWidth(), getFrameHeight(), data, rgba);

    Bitmap bmp = mBitmap; 
    bmp.setPixels(rgba, 0, getFrameWidth(), 0, 0, getFrameWidth(), getFrameHeight());
    return bmp;
}

public native void FindFeatures(int width, int height, byte yuv[], int[] rgba);

}

public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {

private Camera              mCamera;
private SurfaceHolder       mHolder;
private SurfaceView         mViewer;
private int                 mFrameWidth;
private int                 mFrameHeight;
private byte[]              mFrame;
private boolean             mThreadRun;
private byte[]              mBuffer;


public SampleViewBase(Context context) {
    super(context);
    mViewer = (SurfaceView)this.findViewById(R.id.camera_view);
    mHolder = mViewer.getHolder();
    mHolder.addCallback(this);
}

public int getFrameWidth() {
    return mFrameWidth;
}

public int getFrameHeight() {
    return mFrameHeight;
}

public boolean openCamera() {
    releaseCamera();
    mCamera = Camera.open();
    if(mCamera == null)
        return false;

    mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            synchronized (SampleViewBase.this) {
                System.arraycopy(data, 0, mFrame, 0, data.length);
                SampleViewBase.this.notify(); 
            }
            camera.addCallbackBuffer(mBuffer);
        }
    });
    return true;
}

public void releaseCamera() {
    mThreadRun = false;
    synchronized (this) {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }
    onPreviewStopped();
}

public void setupCamera(SurfaceHolder holder,int width, int height) {
    synchronized (this) {
        if (mCamera != null) {
            Camera.Parameters params = mCamera.getParameters();
            List<Camera.Size> sizes = params.getSupportedPreviewSizes();
            mFrameWidth = width;
            mFrameHeight = height;

            // selecting optimal camera preview size
            {
                int  minDiff = Integer.MAX_VALUE;
                for (Camera.Size size : sizes) {
                    if (Math.abs(size.height - height) < minDiff) {
                        mFrameWidth = size.width;
                        mFrameHeight = size.height;
                        minDiff = Math.abs(size.height - height);
                    }
                }
            }

            params.setPreviewSize(getFrameWidth(), getFrameHeight());

            List<String> FocusModes = params.getSupportedFocusModes();
            if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
            {
                params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
            }            

            mCamera.setParameters(params);

            /* Now allocate the buffer */
            params = mCamera.getParameters();
            int size = params.getPreviewSize().width * params.getPreviewSize().height;
            size  = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
            mBuffer = new byte[size];
            /* The buffer where the current frame will be copied */
            mFrame = new byte [size];
            mCamera.addCallbackBuffer(mBuffer);

            try {
                mCamera.setPreviewDisplay(holder);
                //mCamera.setPreviewDisplay(null);
            } catch (IOException e) {}

            /* Notify that the preview is about to be started and deliver preview size */
            onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);

            /* Now we can start a preview */
            mCamera.startPreview();
        }
    }
}

public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
    setupCamera(_holder,width, height);
}

public void surfaceCreated(SurfaceHolder holder) {
    (new Thread(this)).start();
}

public void surfaceDestroyed(SurfaceHolder holder) {
    releaseCamera();
}


//abstract functions used by child class
protected abstract Bitmap processFrame(byte[] data);
protected abstract void onPreviewStarted(int previewWidtd, int previewHeight);
protected abstract void onPreviewStopped();
//================================

public void run() {
    mThreadRun = true;
    while (mThreadRun) {
        Bitmap bmp = null;

        synchronized (this) {
            try {
                this.wait();
                bmp = processFrame(mFrame);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (bmp != null) {
            Canvas canvas = mHolder.lockCanvas();
            if (canvas != null) {
                canvas.drawBitmap(bmp, (canvas.getWidth() - getFrameWidth()) / 2, (canvas.getHeight() - getFrameHeight()) / 2, null);
                mHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

}

我知道这一定是一个重大阻力要经过我的code,但我真的需要帮助。甚至如果我能得到一个链接到该类型的实现的工作示例。另外,请就是不给我这个链接(它并不能帮助我):<一href=\"http://opencv.itseez.com/doc/tutorials/introduction/android_binary_package/android_binary_package.html\"相对=nofollow> OpenCV的自定义应用程序

I know this must be a MAJOR drag to go through my code, but I really need the help. Or even if I could get a link to a working example of this type of implementation. Also, please just don't send me this link (it doesn't help me):openCV in custom applications

推荐答案

这是我的acitivity_main.xml

This is my acitivity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <org.opencv.android.JavaCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/tutorial1_activity_java_surface_view"
        opencv:show_fps="true"
        opencv:camera_id="any" />

    <org.opencv.android.NativeCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/tutorial1_activity_native_surface_view"
        opencv:show_fps="true"
        opencv:camera_id="any" />

    <Button
        android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="105dp"
        android:layout_marginTop="139dp"
        android:onClick="OKClicked"
        android:text="@string/OK" />

    <TextView
        android:id="@+id/txtDisp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnOK"
        android:layout_alignBottom="@+id/btnOK"
        android:layout_marginLeft="25dp"
        android:layout_toRightOf="@+id/btnOK"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

和这些code必须是编辑以MainActivity.java类

And these code must be edited to MainActivity.java class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    if (mIsJavaCamera){
        mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.tutorial1_activity_java_surface_view);
    }else{
        mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.tutorial1_activity_native_surface_view);
    }
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

    ArrayList<View> views = new ArrayList<View>();
    views.add(findViewById(R.id.btnOK));
    views.add(findViewById(R.id.txtDisp));
    mOpenCvCameraView.addTouchables(views);
}

public void OKClicked(View view){
    TextView disp = (TextView)findViewById(R.id.txtDisp);
    disp.setText("OK Clicked");
}

这code被修改为OpenCV的教程1。

This code is modified to OpenCV Tutorial 1.

您会看到一个按钮,并在surfaceview一个TextView。当您单击确定按钮的TextView将显示OK单击的。这是为我工作的三星Galaxy。

You will see a button and a TextView over the surfaceview. When you click OK button TextView will show "OK Clicked". This is working for me on Samsung Galaxy.

这篇关于OpenCV的实施与自定义布局(上SurfaceView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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