所以我的MainActivity让相机在它上面,然后是一个按钮。当按下按钮时,它应该通向TheGuy.java,摄像机也应该在那里工作。
当按下按钮时,我得到了#34;不幸的是应用程序停止了#34;在我的联想A319手机上(Android 4.4.2)。
清单已设定。
这是MainActivity的代码:
package com.example.freewaresys.theguy2;
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
android.hardware.Camera camera;
SurfaceView camView;
SurfaceHolder surfaceHolder;
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
View decorView = getWindow().getDecorView();
if (hasFocus) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camView = (SurfaceView) findViewById(R.id.camerapreview);
surfaceHolder = camView.getHolder();
surfaceHolder.addCallback((SurfaceHolder.Callback) this);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
camera = android.hardware.Camera.open();
camera.setDisplayOrientation(90);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
camera = null;
}
public void TheGuy(View view) {
Intent intent = new Intent(MainActivity.this, TheGuy.class);
startActivity(intent);
camera.setPreviewCallback(null);
camera.stopPreview();
}
}
这是目标TheGuy.java
package com.example.freewaresys.theguy2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import java.io.IOException;
public class TheGuy extends AppCompatActivity implements SurfaceHolder.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_guy);
camView = (SurfaceView) findViewById(R.id.camerapreviewguy);
surfaceHolder = camView.getHolder();
surfaceHolder.addCallback(this);
}
public TheGuy() {
}
android.hardware.Camera camera;
SurfaceView camView;
SurfaceHolder surfaceHolder;
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
View decorView = getWindow().getDecorView();
if (hasFocus) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
camera = android.hardware.Camera.open();
camera.setDisplayOrientation(90);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
}
}
我有一个openCV类,当我按下按钮导致它时,应用程序正常工作。以下是代码:
package com.example.freewaresys.theguy2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class cvCamera extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
JavaCameraView javaCameraView;
Mat mRgba, imgGray, imgCanny;
BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case BaseLoaderCallback.SUCCESS: {
javaCameraView.enableView();
break;
}
default: {
super.onManagerConnected(status);
break;
}
}
super.onManagerConnected(status);
}
};
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
View decorView = getWindow().getDecorView();
if (hasFocus) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cvcamera_layout);
javaCameraView = (JavaCameraView) findViewById(R.id.java_camera_view);
javaCameraView.setVisibility(SurfaceView.VISIBLE);
javaCameraView.setCvCameraViewListener(this);
}
@Override
protected void onPause(){
super.onPause();
if (javaCameraView!=null)
javaCameraView.disableView();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (javaCameraView!=null)
javaCameraView.disableView();
}
@Override
protected void onResume() {
super.onResume();
if (OpenCVLoader.initDebug()){
mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
else{
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallBack);
}
}
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(width, height, CvType.CV_8UC4);
imgGray = new Mat(width, height, CvType.CV_8UC1);
imgCanny = new Mat(width, height, CvType.CV_8UC1);
}
@Override
public void onCameraViewStopped() {
mRgba.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
Imgproc.cvtColor(mRgba, imgGray, Imgproc.COLOR_RGB2GRAY);
Imgproc.Canny(imgGray, imgCanny, 80, 255);
return imgCanny;
}
}
编辑:LogCat打印太多行。我让它只显示错误,现在我正在复制日志的顶部。希望这就足够了:
08-16 21:06:16.555 1574-1574/com.example.freewaresys.theguy2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.freewaresys.theguy2, PID: 1574
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:469)
at android.hardware.Camera.open(Camera.java:442)
at com.example.freewaresys.theguy2.TheGuy.surfaceCreated(TheGuy.java:54)
at android.view.SurfaceView.updateWindow(SurfaceView.java:637)
at android.view.SurfaceView.access$000(SurfaceView.java:90)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:193)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2343)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6420)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
好的,所以我从MainActivity中删除了相机,现在只有按钮了。 而我Asynk像这样在TheGuy中拍摄相机(一切正常):
public class TheGuy extends AppCompatActivity implements SurfaceHolder.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_guy);
camView = (SurfaceView) findViewById(R.id.camerapreviewguy);
surfaceHolder = camView.getHolder();
surfaceHolder.addCallback(this);
}
// public TheGuy() {
//
// }
Camera camera = null;
SurfaceView camView;
SurfaceHolder surfaceHolder;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
View decorView = getWindow().getDecorView();
if (hasFocus) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
new ShowCameraTask().execute();
camera.setDisplayOrientation(90);
}
private class ShowCameraTask extends AsyncTask<Object, Object, Camera> {
@Override
protected Camera doInBackground(Object... params) {
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
} else {
camera = Camera.open();
}
return camera;
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
new ShowCameraTask().execute();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
}
}