- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个实现了一些 OpenCV 的 Android Activity 。它的作用是简单地创建一个自定义相机 View ,当触摸屏幕时,照片将被保存。
我唯一的问题是 BaseLoaderCallback 中的代码 mOpenCvCameraView.setOnTouchListener(MainActivity.this);
包含警告。
mOpenCvCameraView.setOnTouchListener(MainActivity.this);
警告
Custom view com/example/zcameratestv2/Version2CameraView has setOnTouchListener called on it but does not override performClick
与其他问题不同,我的类(class)扩展了一个 Activity 而不是 View,所以当我尝试重写函数时 private boolean performClick() { ...super.performClick(); }
无法识别。这是我的类(class)
package com.example.zcameratestv2;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.core.Mat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Toast;
public class MainActivity extends Activity implements CvCameraViewListener, OnTouchListener {
private Version2CameraView mOpenCvCameraView;
private static final String TAG = "Version 2::Activity";
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
mOpenCvCameraView.setOnTouchListener(MainActivity.this);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public MainActivity() {
Log.i(TAG, "Version 2 Class instantiated");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (Version2CameraView) findViewById(R.id.java_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCameraViewStarted(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void onCameraViewStopped() {
// TODO Auto-generated method stub
}
@Override
public Mat onCameraFrame(Mat inputFrame) {
// TODO Auto-generated method stub
return null;
}
@SuppressLint("SimpleDateFormat")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//some code....
break;
case MotionEvent.ACTION_UP:
Log.i(TAG,"onTouch event");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String currentDateandTime = sdf.format(new Date());
String fileName = Environment.getExternalStorageDirectory().getPath() +
"/sample_picture_" + currentDateandTime + ".jpg";
mOpenCvCameraView.takePicture(fileName);
Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
v.performClick();
break;
default:
break;
}
return true;
}
}
以前 public boolean onTouch(View v, MotionEvent event)
事件有一个类似于 OnTouchListener 的警告,它显示我应该使用 performClick();方法,但我无法覆盖它,因为我扩展到 Activity
而不是 View
。我发现应用 v.PerformClick();
可以解决这个问题。
这个其他类处理相机进程
package com.example.zcameratestv2;
import java.io.FileOutputStream;
import org.opencv.android.JavaCameraView;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.AttributeSet;
import android.util.Log;
public class Version2CameraView extends JavaCameraView implements PictureCallback {
private static final String TAG = "Version2::CameraView";
private String mPictureFileName;
public Version2CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void takePicture(final String fileName) {
Log.i(TAG, "Taking picture");
this.mPictureFileName = fileName;
// Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
// Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
mCamera.setPreviewCallback(null);
// PictureCallback is implemented by the current class
mCamera.takePicture(null, null, this);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Saving a bitmap to file");
// The camera preview was automatically stopped. Start it again.
mCamera.startPreview();
mCamera.setPreviewCallback(this);
// Write the image in a file (in jpeg format)
try {
FileOutputStream fos = new FileOutputStream(mPictureFileName);
fos.write(data);
fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}
}
我已在 list 文件中包含所需的权限,例如 CAMERA 和 WRITE_EXTERNAL_STORAGE
有人可以确定问题所在吗?需要你的帮助。提前致谢!
最佳答案
onTouch()
方法从未标记为“已处理”的底层 View 中获取每个触摸事件。如果您的 Version2CameraView
不处理触摸事件,它们将在 Activity 中处理,并且您的 Version2CameraView
将作为 View v
参数传递。
不幸的是,您的 JavaCameraView
没有覆盖 performClick()
,但您正在尝试在此 View 上调用它。解决方案?将此方法添加到您的 Version2CameraView
类:
@Override
public boolean performClick() {
// do what you want
return true;
}
关于java - 自定义 View 调用了 setOnTouchListener 但没有覆盖 performClick(类扩展 Activity NOT View),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32065086/
我是一名优秀的程序员,十分优秀!