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"
    >
    <Button android:layout_width="match_parent" android:text="打开按钮" android:id="@+id/button1" android:layout_height="wrap_content"></Button>
    <Button android:layout_width="match_parent" android:text="关闭按钮" android:id="@+id/button2" android:layout_height="wrap_content"></Button>
    <Button android:layout_width="match_parent" android:text="拍照按钮" android:id="@+id/button3" android:layout_height="wrap_content"></Button>
	<SurfaceView android:id="@+id/mySurfaceView" 
		android:gravity="center_horizontal" android:layout_width="fill_parent"
		android:layout_height="300px" />
	<ImageView android:id="@+id/myImageView"
		android:layout_width="fill_parent" android:layout_height="300px" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestActivity" 
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" />   
 	<uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>

TestActivity.java主文件

package com.test;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class TestActivity extends Activity implements Callback, OnClickListener {
    /** Called when the activity is first created. */
    SurfaceView mySurfaceView;// SurfaceView的引用
    SurfaceHolder mySurfaceHolder;// SurfaceHolder的引用
    Button button1;// 打开按钮
    Button button2;// 关闭按钮
    Button button3;// 拍照按钮
    Camera myCamera;// Camera的引用
    boolean isView = false;// 是否在浏览中
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
         //设置全屏显示
         requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        mySurfaceView = (SurfaceView) findViewById(R.id.mySurfaceView);
        mySurfaceHolder = mySurfaceView.getHolder();              //此对象用于在Camera和SurfaceView之间传递数据
        mySurfaceHolder.addCallback(this);                                 
        mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void initCamera() {               //初始化相机
        if (!isView) {
            myCamera = Camera.open(); 
        }
        if (myCamera != null && !isView) {
            try {
                Camera.Parameters myParameters = myCamera.getParameters();
                myParameters.setPictureFormat(PixelFormat.JPEG);              //设置照片格式
                myParameters.set("orientation", "portrait");
//                myParameters.setPreviewSize(dm.widthPixels/2,dm.heightPixels/2);                                //大小
                myCamera.setParameters(myParameters);        
                myCamera.setPreviewDisplay(mySurfaceHolder);      
                myCamera.startPreview();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            isView = true;
        }
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==button1){
            initCamera();
        }else if(v==button2){
            isView=false;
            myCamera.stopPreview();
            myCamera.release();
            myCamera=null;
        }else if(v==button3){
            myCamera.takePicture(mShutterCallback, myRawCallback, myjpegCallback);      //进行照相
        }
    }

    ShutterCallback mShutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {
            // TODO Auto-generated method stub
        }
    };

    PictureCallback myRawCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
        }
    };
    PictureCallback myjpegCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {               //将照下来的图片用ImageView显示
            // TODO Auto-generated method stub
            Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
            ImageView myImageView=(ImageView) findViewById(R.id.myImageView);
            myImageView.setImageBitmap(bm);
            isView=false;
            myCamera.stopPreview();
            myCamera.release();
            myCamera=null;
            initCamera();
        }
    };

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }
}

相关推荐