Android Game Development Study Notes (4): Camera image acquisition

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.