拍照必须设置权限:
<uses-permission android:name="android.permission.CAMERA"/>
1、布局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" android:gravity="center_horizontal" > <SurfaceView android:id="@+id/mySurfaceView" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="200px" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/btnOpen" android:textSize="18px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开" /> <Button android:id="@+id/btnClose" android:textSize="18px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭" /> <Button android:id="@+id/btnTake" android:textSize="18px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" /> </LinearLayout>
<ImageView android:id="@+id/myImageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>2、初始化组件:private SurfaceView mySurfaceView = null;//SurfaceView的引用private SurfaceHolder mySurfaceHolder = null;//SurfaceHolder的引用private Button btnOpen = null;//打开按钮private Button btnClose = null;//关闭按钮private Button btnTake = null;//拍照按钮
添加监听器:OnClickListener, SurfaceHolder.Callback super.onCreate(savedInstanceState);
//全屏 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); mySurfaceView = (SurfaceView) findViewById(R.id.mySurfaceView);//得到SurfaceView的引用 btnOpen = (Button) findViewById(R.id.btnOpen);//得到按钮的引用 btnClose = (Button) findViewById(R.id.btnClose);//得到按钮的引用 btnTake = (Button) findViewById(R.id.btnTake);//得到按钮的引用 btnOpen.setOnClickListener(this);//为按钮添加监听 btnClose.setOnClickListener(this);//为按钮添加监听 btnTake.setOnClickListener(this);//为按钮添加监听 mySurfaceHolder = mySurfaceView.getHolder();//获得SurfaceHolder mySurfaceHolder.addCallback(this);//添加接口的实现 mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
3、使用CAMERA拍照 private Camera myCamera = null;//Camera的引用 boolean isView = false;//是否在浏览中
1)、初始化照相机:
public void initCamera(){ if(!isView){ myCamera = Camera.open(); } if(myCamera != null && !isView){ try { Camera.Parameters myParameters = myCamera.getParameters(); myParameters.setPictureFormat(PixelFormat.JPEG); //真机删除setPreviewSize方法 myParameters.setPreviewSize(200, 200);//屏幕大小 myCamera.setParameters(myParameters); myCamera.setPreviewDisplay(mySurfaceHolder); myCamera.startPreview();//立即运行Preview } catch (IOException e) {//捕获异常 e.printStackTrace();//打印错误信息 } isView = true; } }
2、打开/关闭和拍照 @Overridepublic void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { //打开照相机 case R.id.btnOpen: initCamera(); break; case R.id.btnClose: if(myCamera != null && isView){//当正在显示时 isView = false; myCamera.stopPreview(); myCamera.release(); myCamera = null; } break; //拍照 case R.id.btnTake: myCamera.takePicture(myShutterCallback, myRawCallback, myjpegCallback); break; }}//相机快门关闭ShutterCallback myShutterCallback = 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) { Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); ImageView myImageView = (ImageView) findViewById(R.id.myImageView); myImageView.setImageBitmap(bm);//将图片显示到下方的ImageView中 isView = false; myCamera.stopPreview(); myCamera.release(); myCamera = null; initCamera();//初始化相机 }};