`

MyCameraActivity

阅读更多

package cn.fn;

import android.app.Activity;
import android.os.Bundle;

public class MyCameraActivity extends Activity {
    /** Called when the activity is first created. */
    MySurfaceView mySurface;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mySurface = new MySurfaceView(this);
        setContentView(mySurface);
    }

}

 

package cn.fn;

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements
        SurfaceHolder.Callback {
    SurfaceHolder holder;
    Camera myCamera;

    public MySurfaceView(Context context) {
        super(context);
        holder = getHolder();// 获得surfaceHolder引用
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 设置类型
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        myCamera.startPreview();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if (myCamera == null) {
            myCamera = Camera.open();// 开启相机,不能放在构造函数中,不然不会显示画面.
            try {
                myCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        myCamera.stopPreview();// 停止预览
        myCamera.release();// 释放相机资源
        myCamera = null;
    }
}

 

加权限:<uses-permission android:name="android.permission.CAMERA"/>

           不然会出:con‘t connect camera service

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics