【自定义Android相机】

调用camera API拍照流程:

1. 首先必须使用surfaceView画布,并且需要继承surfaceHolder callback

在Activity的OnCreate函数中设置好SurfaceView,包括设置SurfaceHolder.Callback对象和SurfaceHolder对象的类型,具体如下:
SurfaceView mpreview = (SurfaceView) this.findViewById(R.id.camera_preview);
SurfaceHolder mSurfaceHolder = mpreview.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

2. 关键部分surfaceChangedsurfaceCreated,和surfaceDestroyed编写:

  • 初始化相机,在surfaceChanged进行

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//定义参数
Camera.Parameters parameters = mCamera.getParameters();
//设置照片的格式
parameters.setPictureFormat(PixelFormat.JPEG);
//设置照片的大小,对于高版本的相机不用推荐直接设置,因为相机可能不支持下面讨论
parameters.setPictureSize(320, 480);
//这句对于2.2以下必须,2.3不需要这句
mCamera.setParameters(parameters);
//开始预览
mCamera.startPreview();
}

  • surfaceCreated部分setPreviewDisplay为摄像头设置SurfaceHolder对象

public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open(); //打开摄像头
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
mCamera.release();//一定要释放掉相机资源,不然相机在返回以后再拍照时,非常有可能会报错
mCamera = null;
}
}

  • 对于surfaceDestroyed部分,调用Camera::stopPreview函数停止预览功能,并且调用Camera::release函数释放Camera,以便其他应用程序调用。

// 预览界面被关闭时,或者停止相机拍摄;释放相机资源
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
if(mCamera!=null) mCamera.release();
mCamera = null;
}

3. 在拍照的时候,调用takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)函数来完成拍照

    public void takePicture() {
        if(mCamera != null) {
            mCamera.takePicture(mShutterCallback, null, jpegCallback);
        }
    }
Parameters
shutter the callback for image capture moment, or null
raw the callback for raw (uncompressed) image data, or null
postview callback with postview image data, may be null
jpeg the callback for JPEG image data, or null

四个回调接口(The camera service will initiate a series of callbacks to the application as the image capture progresses):当开始拍照时,会依次调用shutter的onShutter()方法,raw的onPictureTaken方法,jpeg的onPictureTaken方法.三个参数的作用是shutter–拍照瞬间调用,raw–获得没有压缩过的图片数据,jpeg—返回jpeg的图片数据

    • ShutterCallback是快门按下的回调, e.g. 可以在onShutter() 中设置播放“咔嚓”声之类的操作

private final ShutterCallback mShutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d("ShutterCallback", "...onShutter...");
}
};

  • 三个PictureCallback接口分别对应三份图像数据, 原始图像、缩放和压缩图像, JPG图像,图像数据可以在PictureCallback接口的void onPictureTaken(byte[] data, Camera camera)中获得
  • 通常我们只关心JPG图像数据,此时前面两个PictureCallback接口参数可以直接传null;当你不需要对照片进行处理,可以直接用null代替
  • 当调用camera.takePiture方法后,camera关闭了预览,这时需要调用startPreview()来重新开启预览。
private PictureCallback jpegCallback = new PictureCallback() {        
        @Override //实例化了一个本界面的PictureCallback, 当用户拍完一张照片的时候触发,这时候对图片处理并保存操作
        public void onPictureTaken(byte[] data, Camera camera) {
            cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            File myCaptureFile = new File("/sdcard/camera/camera1.jpg");
            try {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
                cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                bos.flush();
                bos.close();
                Canvas canvas = mHolder.lockCanvas();
                canvas.drawBitmap(cameraBitmap, 0, 0, null);
                mHolder.unlockCanvasAndPost(canvas);
            }
            catch (Exception e) {
                e.getStackTrace();
            }
        }
    };

每次调用takePicture获取图像后,摄像头会停止预览,假如需要继续拍照,则我们需要在上面的PictureCallback的onPictureTaken函数末尾,再次掉用Camera::startPreview函数;

via:

Android拍照,上传,预览综合【修改】

Android Camera 使用小结

Android相机的实现

系统camera的uri为:android.media.action.IMAGE_CAPTURE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: