这是我的整个活动代码: import java.io.IOException;
import pickld.android.intents.R;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class CameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
int numberOfCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button cancel = (Button)findViewById(R.id.cancel);
Button openGallery = (Button)findViewById(R.id.openGallery);
Button takePhoto = (Button)findViewById(R.id.capture);
Button switchCam = (Button)findViewById(R.id.flip);
Button toggleFlash = (Button)findViewById(R.id.toggleFlash);
cancel.setOnClickListener(this);
openGallery.setOnClickListener(this);
takePhoto.setOnClickListener(this);
switchCam.setOnClickListener(this);
toggleFlash.setOnClickListener(this);
}
public void openFrontFacingCamera() {
numberOfCamera = Camera.getNumberOfCameras();
if(camId == Camera.CameraInfo.CAMERA_FACING_BACK){
camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
Toast.makeText(getApplicationContext(), "BACK TO FRONT" ,
1000).show();
try {
camera = Camera.open(camId);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (RuntimeException e) {
} catch (IOException e) {}
}else if(camId == Camera.CameraInfo.CAMERA_FACING_FRONT){
camId = Camera.CameraInfo.CAMERA_FACING_BACK;
Toast.makeText(getApplicationContext(), "FRONT TO BACK" ,
1000).show();
try {
camera = Camera.open(camId);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (RuntimeException e) {
} catch (IOException e) {}
}
}
@Override
public void onClick(View click) {
if(click.getId() == R.id.cancel){
this.finish();
}else if(click.getId() == R.id.flip){
openFrontFacingCamera();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open(camId);
camera.setDisplayOrientation(90);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
每次我调用该openFrontFacingCamera()
方法时,每个条件似乎都运行良好,我可以在Toast
我放在那里的地方看到它,但相机本身没有切换。我认为这条线一定有什么遗漏或错误,camera = Camera.open(camId);
我不确定。请帮我。谢谢。