android --相机使用教程(2)
续:http://topic.csdn.net/u/20120518/14/00457380-e333-46f8-9d7f-8eee0d82e3b9.html?45849
转自:http://dev.10086.cn/cmdn/bbs/thread-39389-1-2.html
(5)能够预览了,接下来就是拍照了,拍照用到了一个camera.tackPiture()这个方法,这个方法,有三个参数分别是
ShutterCallBack shutter,PictureCallBack raw,PictureCallBack jpeg.
下面是对他们的实现
01 private ShutterCallback shutter = new ShutterCallback() {
02
03 @Override
04 public void onShutter() {
05 // TODO Auto-generated method stub
06 Log.d("ddd", "shutter");
07
08 }
09 };
10 private PictureCallback raw = new PictureCallback() {
11
12 @Override
13 public void onPictureTaken(byte[] data, Camera camera) {
14 // TODO Auto-generated method stub
15 Log.d("ddd", "raw");
16
17 }
18 };
19 private PictureCallback jpeg = new PictureCallback() {
20
21 @Override
22 public void onPictureTaken(byte[] data, Camera camera) {
23 // TODO Auto-generated method stub
24 Log.d("ddd","jpeg");
25
26 }
27 };
当开始拍照时,会依次调用shutter的onShutter()方法,raw的onPictureTaken方法,jpeg的onPictureTaken方法.
三个参数的作用是shutter--拍照瞬间调用,raw--获得没有压缩过的图片数据,jpeg---返回jpeg的图片数据
当你不需要对照片进行处理,可以直接用null代替.
注意,当调用camera.takePiture方法后,camera关闭了预览,这时需要调用startPreview()来重新开启预览。
我用以上知识,加到上面的那个例子,就形成了下面的代码:
MySurfaceView.javaview sourceprint?01 package com.wjh.camera;
02 import java.io.IOException;
03 import android.content.Context;
04 import android.graphics.PixelFormat;
05 import android.hardware.Camera;
06 import android.hardware.Camera.PictureCallback;
07 import android.hardware.Camera.ShutterCallback;
08 import android.util.Log;
09 import android.view.SurfaceHolder;
10 import android.view.SurfaceView;
11 public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{
12 SurfaceHolder holder;
13 Camera myCamera;
14 private ShutterCallback shutter = new ShutterCallback() {
15
16 @Override
17 public void onShutter() {
18 // TODO Auto-generated method stub
19 Log.d("ddd", "shutter");
20
21 }
22 };
23 private PictureCallback raw = new PictureCallback() {
24
25 @Override
26 public void onPictureTaken(byte[] data, Camera camera) {
27 // TODO Auto-generated method stub
28 Log.d("ddd", "raw");
29
30 }
31 };
32 private PictureCallback jpeg = new PictureCallback() {
33
34 @Override
35 public void onPictureTaken(byte[] data, Camera camera) {
36 // TODO Auto-generated method stub
37 Log.d("ddd","jpeg");
38
39 }
40 };
41 public MySurfaceView(Context context)
42 {
43 super(context);
44 holder = getHolder();//获得surfaceHolder引用
45 holder.addCallback(this);
46 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置类型
47 }
48 public void tackPicture()
49 {
50 myCamera.takePicture(null,null,null);
51 }
52 public void voerTack()
53 {
54 myCamera.startPreview();
55 }
56 @Override
57 public void surfaceChanged(SurfaceHolder holder, int format, int width,
58 int height) {
59 myCamera.startPreview();
60 }
61 @Override
62 public void surfaceCreated(SurfaceHolder holder) {
63 // TODO Auto-generated method stub
64 if(myCamera == null)
65 {
66 myCamera = Camera.open();//开启相机,不能放在构造函数中,不然不会显示画面.
67 try {
68 myCamera.setPreviewDisplay(holder);
69 } catch (IOException e) {
70 // TODO Auto-generated catch block
71 e.printStackTrace();
72 }
73 }
74 }
75 @Override
76 public void surfaceDestroyed(SurfaceHolder holder) {
77 // TODO Auto-generated method stub
78 myCamera.stopPreview();//停止预览
79 myCamera.release();//释放相机资源
80 myCamera = null;
81
82 }
83 }
CameraTest_3.javaview sourceprint?01 import android.app.Activity;
02 import android.os.Bundle;
03 import android.view.View;
04 import android.view.View.OnClickListener;
05 public class CameraTest_3 extends Activity implements OnClickListener {
06 /** Called when the activity is first created. */
07 MySurfaceView mySurface;
08 boolean isClicked = false;
09 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 mySurface = new MySurfaceView(this);
13 setContentView(mySurface);
14 mySurface.setOnClickListener(this);
15 }
16 @Override
17 public void onClick(View v) {
18 // TODO Auto-generated method stub
19 if(!isClicked)
20 {
21 mySurface.tackPicture();
22 isClicked = true;
23 }else
24 {
25 mySurface.voerTack();
26 isClicked = false;
27
28 }
29
30 }
这样就是实现了拍照的功能,那么怎样要图片保存呢?那么这是就需要在那个参数中的jpeg的
方法里面进行处理了,那个方法的data参数,就是相片的数据。
我们通过BitmapFactory.decodeByteArray(data, 0, data.length)来获得图片并通过io处理,将图片保存到想要保存的位置
未完待续!
[解决办法]
看看使用教程