正文  多媒体 > 摄像头/Camera >

Android游戏开发学习笔记(四):Camera图像采集

一、为相机添加权限。在AndroidManifest.xml文件的</application>之后,添加相机使用的权限: <uses-permission android:name="a...

一、为相机添加权限。在AndroidManifest.xml文件的</application>之后,添加相机使用的权限:
<uses-permission android:name="android.permission.CAMERA"/>
二、编写布局文件main.xml,代码如下:
<?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="打开"  
            />
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭"
            />
    </LinearLayout>
</LinearLayout>
三、编写MainActivity.java代码。
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 
    } 
}
 
作者 “Android学习心得”