1. add permissions to the camera. After the AndroidManifest. xml file </application>, add the camera permission:
<Uses-permission android: name = "android. permission. CAMERA"/>
2. Compile the layout file main. xml with the following code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<SurfaceView
Android: id = "@ + id/surfaceView"
Android: layout_width = "320px"
Android: layout_height = "240px"
/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
<Button
Android: id = "@ + id/btn1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "open"
/>
<Button
Android: id = "@ + id/btn2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "disabled"
/>
</LinearLayout>
</LinearLayout>
3. Compile MainActivity. java code.
Package game. test;
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. widget. Button;
Public class MainActivity extends Activity implements SurfaceHolder. Callback {
Camera myCamera;
SurfaceView mySurfaceView;
SurfaceHolder mySurfaceHolder;
Button btn1, btn2;
Boolean isPreview = false;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MySurfaceView = (SurfaceView) findViewById (R. id. surfaceView );
Btn1 = (Button) findViewById (R. id. btn1 );
Btn2 = (Button) findViewById (R. id. btn2 );
MySurfaceHolder = mySurfaceView. getHolder ();
MySurfaceHolder. addCallback (this );
MySurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
Btn1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
InitCamera ();
}
});
Btn2.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
If (myCamera! = Null & isPreview ){
MyCamera. stopPreview ();
MyCamera. release ();
MyCamera = null;
IsPreview = false;
}
}
});
}
Public void initCamera (){
If (! IsPreview ){
MyCamera = Camera. open ();
}
If (myCamera! = Null &&! IsPreview ){
Try {
MyCamera. setPreviewDisplay (mySurfaceHolder );
MyCamera. startPreview ();
} Catch (Exception e ){
E. printStackTrace ();
}
IsPreview = true;
}
}
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// TODO Auto-generated method stub
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
// TODO Auto-generated method stub
}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
}
}
Author's "Android Learning Experience"