照相机拍照需要的权限: <!--写sd卡权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--读sd卡的权限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--操作摄像头的权限 --> <uses-permission android:name="android.permission.CAMERA" /> 1.调用系统的拍照功能,获得并保存图片 public void paizhao(View v){ Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//启动摄像头 this.startActivityForResult(i, 1); //启动、并重写返回方法 参数2是返回的识别码 } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && null != data && null != data.getExtras()) {//通过识别码判断是拍照进行的返回 try { Bitmap bitmap=(Bitmap) data.getExtras().get("data");//返回参数中接收图片数据 FileOutputStream fos;//设定输出流对象,准备保存文件 String filename=System.currentTimeMillis()+".jpg";//定义保存文件的名称 fos = new FileOutputStream (Environment.getExternalStorageDirectory()+ "/"+filename);//输出流地址名称 //1.压缩格式、2.压缩质量0-100;3.压缩到文件流 boolean b = bitmap.compress(CompressFormat.JPEG, 20, fos);//图片压缩到本地文件 if(b){ Toast.makeText(this, "保存成功"+filename, 0).show(); }else{ Toast.makeText(this, "保存失败", 0).show(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } 2.通过Camera、SurfaceView、自己实现拍照及预览效果 1)xml中定义 SurfaceView 2)activity中findviewbyid,找到 SurfaceView 3)//surfaceholder,配合监听surfaceview的动态 Callback callback=new SurfaceHolder.Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { //停止预览、销毁布局,释放摄像头 camera.stopPreview(); // 释放硬件资源 camera.release(); camera=null; } @Override public void surfaceCreated(SurfaceHolder holder) { try {//创建布局,初始化摄像头 //打开摄像头 camera=Camera.open(); //设置摄像头预览画面 camera.setPreviewDisplay(sv.getHolder());//holder //设置摄像头的旋转角度 camera.setDisplayOrientation(90); //0表示摄像头初始角度是0向左,90度表示正,向上 camera.startPreview();//启动 } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }; 4)设置回调监听//surfaceiew 监听事件 sv.getHolder().addCallback(callback); //点击拍照,保存图片到本地。1.摄像头自动对焦;2.图回调完成图片存储本地; public void paizhao(View v){ PictureCallback jpeg=new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { String filename=System.currentTimeMillis()+"__.jpg"; FileOutputStream fos = new FileOutputStream (Environment.getExternalStorageDirectory()+ "/"+filename); fos.write(data); fos.flush(); fos.close(); //摄像头预览 camera.startPreview();//保存之后返回预览界面 } catch (Exception e) { e.printStackTrace(); } } }; //摄像头聚焦 camera.autoFocus(null); //摄像头拍照+存储 camera.takePicture(null, null, jpeg); }