public class MainActivity extends ActionBarActivity {
Camera cam = null;//camera object used all over
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Added a button listener
Button mNext=(Button)findViewById(R.id.button1);
mNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Test", "one");
FrontFacingCamera();
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public Camera FrontFacingCamera() {
//opening a camera
Log.e("Test", "Two");
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
Camera.Parameters params = cam.getParameters();
SurfaceView view = new SurfaceView(context);
try{
cam.setPreviewDisplay(view.getHolder());
}catch(IOException e){
throw new RuntimeException(e);
}
cam.startPreview();
} catch (RuntimeException e) {
Log.e("Test", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
//opened a camera
}
}
这里我刚尝试调用我的方法FrontFacingCamera();那么它应该给我预览。但相反,它什么都没给我。我正试图用我的前置摄像头打开并拍摄一张图像,但它没有给我预览,这就是我被击中的原因。