gpt4 book ai didi

android-layout - 带有自定义布局的 opencv 实现(在 SurfaceView 上)

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:51 26 4
gpt4 key购买 nike

我有一个 openCV 应用程序在工作,但需要在布局中添加按钮等。所以基本上我想在 surfaceView 上显示 opencv 相机 View ,并在下面添加其他内容。

我在互联网和论坛上搜索了一段时间,只看到有 opencv 面部检测应用程序的人也想添加自定义布局...没有解决方案。

我真的很想找到解决方案,因此非常感谢您的帮助。为此,我使用了 OpenCV 示例 3 应用程序(作为一个简单示例)并尝试绑定(bind)到简单自定义布局上的表面 View 。我在普通的相机应用程序中管理它,但在 opencv 示例中遇到了很多困难。

所以这是我分别为 Sample3Native.java、Sample3View.java 和 SampleViewBase.java(如示例)文件编写的代码:

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);
}
}
}
}

我知道这对我的代码来说一定是一个主要的拖累,但我真的需要帮助。或者即使我可以获得指向此类实现的工作示例的链接。另外,请不要向我发送此链接(它对我没有帮助):openCV in custom applications

最佳答案

这是我的 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>

并且这些代码必须编辑到 MainActivity.java 类中

@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");
}

此代码修改为OpenCV教程1。

您将在表面 View 上看到一个按钮和一个 TextView。当您单击“确定”按钮时,TextView 将显示“确定已单击”。这在三星 Galaxy 上对我有用。

关于android-layout - 带有自定义布局的 opencv 实现(在 SurfaceView 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com