So habe ich eine sehr einfache Kamera-Implementierung. Mein Ziel ist es, automatisch alle 10 Sekunden zwischen der vorderen und hinteren Kamera umzuschalten, bis Sie auf Knopfdruck stehen bleiben.Automatische und automatische Umschaltung der Kamera
ist hier mein MainActivity
:
public class MainActivity extends Activity {
private Camera mCamera = null;
private CameraView mCameraView = null;
private CountDownTimer countDownTimer;
private int mCamId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startCam();
if(mCamera != null) {
mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
//btn to close the application
ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.exit(0);
}
});
}
private void startCam() {
try{
//you can use open(int) to use different cameras
mCamId = mCamId == 0 ? 1 : 0;
mCamera = Camera.open(mCamId);
switchCam();
} catch (Exception e){
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
}
private void switchCam() {
//10 seconds
countDownTimer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long l) {
Log.d(TAG, l + " left");
}
@Override
public void onFinish() {
cleanup();
startCam();
}
}.start();
}
public void cleanup() {
Log.i(TAG, "Switching Camera");
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
}
Und hier ist meine CameraView
Klasse:
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraView(Context context, Camera camera){
super(context);
mCamera = camera;
mCamera.setDisplayOrientation(90);
//get the holder and set this class as the callback, so we can get camera data here
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try{
//when the surface is created, we can set the camera to draw images in this surfaceholder
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
//before changing the application orientation, you need to stop the preview, rotate and then start it again
if(mHolder.getSurface() == null)//check if the surface is ready to receive camera data
return;
try{
mCamera.stopPreview();
} catch (Exception e){
//this will happen when you are trying the camera if it's not running
}
//now, recreate the camera preview
try{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
//our app has only one screen, so we'll destroy the camera in the surface
//if you are unsing with more screens, please move this code your activity
mCamera.stopPreview();
mCamera.release();
}
Die erste Kamera ohne Probleme gestartet. Die Schnittstelle friert jedoch beim Umschalten der zweiten Kamera, also nach 10 Sekunden, in ein statisches Bild ein. Ich kann es nicht beheben. Wo irre ich mich?
Minimal, Complete, prüfbare und kompilierbare-Code: LINK
Wird die gesamte Anwendung eingefroren? Gibt es etwas Nützliches in der Logcat-Ausgabe? Der Link zum mitgelieferten Quellcode ist tot! – Al0x
@ Al0x Ich habe den Link sofort aktualisiert! Nein, nur die Kamera friert ein! –