我想在android studio中使用opencv创建一个用于图像处理的应用程序。我没有使用Android Java编码的经验。这就是为什么我想得到很多建议。当前项目是从服务中的摄像机图像接收帧,而不是主要活动,并处理图像。这里的问题是后台服务必须能够进行图像处理。当前型号为nexus 5.当您启动应用程序时,会出现相机图像。此时,如果执行主页按钮或其他应用程序,则应在后台连续输入图像数据,并且应该可以进行图像处理。我现在有很多搜索结果,而且我已经得到了表面纹理可以做到的答案。但是,无论您如何使用surfacetexture运行其他应用程序,在后台使用图像数据进行图像处理 我不知道我是否能够成功。以下是我的主要活动和服务。
public class MainActivity extends Activity{
private static final String TAG = MainActivity.TAG;
private Camera mCamera;
public static TextureView mTextureView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextureView = new TextureView(this);
setContentView(mTextureView);
setContentView(R.layout.activity_main);
startService(new Intent(this, VisionService.class));
}
}
public class VisionService extends Service implements TextureView.SurfaceTextureListener {
private static final String TAG = VisionService.class.getName();
private WindowManager windowManager;
private Camera mCamera = null;
private TextureView mTextureView;
private SurfaceTexture mSurfaceTexture;
private float[] mTransformMatrix;
public static Vibrator mVibrator;
private final IBinder mBinder = new LocalBinder();
@Override
public void onCreate() {
super.onCreate();
try {
mTextureView = MainActivity.mTextureView;
mTextureView.setSurfaceTextureListener(this);
Log.i("ABC","onCreate");
} catch (Exception e) {
Log.i("ABC","onCreate exception "+e.getMessage());
e.printStackTrace();
}
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "surfaceDestroyed method");
}
public class LocalBinder extends Binder {
VisionService getService() {
// Return this instance of this service so clients can call public methods
return VisionService.this;
}
}//end inner class that returns an instance of the service.
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}//end onBind.
@Override
public void onSurfaceTextureAvailable(SurfaceTexture mSurfaceTexture, int width, int height) {
mCamera = Camera.open(1);
if (mCamera == null) {
// Seeing this on Nexus 7 2012 -- I guess it wants a rear-facing camera, but
// there isn't one. TODO: fix
throw new RuntimeException("Default camera not available");
}
try {
mCamera.setPreviewTexture(mSurfaceTexture);
mCamera.startPreview();
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (data == null) return;
Camera.Size size = mCamera.getParameters().getPreviewSize();
if (size == null) return;
//This is where I start my thread that analyzes images
Log.i("TAG","Hayoon");
}
});
} catch (IOException ioe) {
// Something bad happened
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}