android 拍照

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    Camera camera = Camera.open();
    Parameters params = camera.getParameters();
    List<Camera.Size> previewSize = params.getSupportedPreviewSizes();
    List<Camera.Size> pictureSize = params.getSupportedPictureSizes();
    if(previewSize.size()>0 && pictureSize.size()>0)
    {
     Camera.Size maxPreviewSize = previewSize.get(previewSize.size()-1);
     params.setPreviewSize(maxPreviewSize.width,maxPreviewSize.height);//设置预览照片的大小
     params.setPictureFormat(PixelFormat.JPEG);// 设置图片格式
     params.setJpegQuality(100);//照片质量
     
     Camera.Size maxPictureSize = pictureSize.get(pictureSize.size()-1);
     params.setPictureSize(maxPictureSize.width,maxPictureSize.height);//设置照片的大小
 
     //旋转90度 
     if (Integer.parseInt(Build.VERSION.SDK) >= 8)
         setDisplayOrientation(camera, 90);  
     else params.setRotation(90);
 
     camera.setParameters(params);
     try {
      camera.setPreviewDisplay(surfaceView.getHolder());
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }//通过SurfaceView显示取景画面
     camera.startPreview();
     camera.autoFocus(null);//自动对焦
     camera.takePicture(null, null, pictureCallback);
     camera.startPreview();
    } 
 /**
 * 拍照的回调接口
 */
 PictureCallback pictureCallback = new PictureCallback()
 {
  @Override
  public void onPictureTaken(byte[] data, Camera camera)
  {
   if (data != null)
   {
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
    File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
    FileOutputStream outStream = null;
    try
    {
     outStream = new FileOutputStream(file);
     bitmap.compress(CompressFormat.JPEG, 100, outStream);
     outStream.close();
     Intent sendIntent = new Intent(Intent.ACTION_SEND,  Uri.parse("mms://"));
     sendIntent.setType("image/jpeg");
     sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
     startActivity(Intent.createChooser(sendIntent, "MMS:"));
    }
    catch (Exception e)
    {
     e.printStackTrace();
    }
   }
   camera.stopPreview();
   camera.release();
   camera = null;
  }
 }; 
    protected void setDisplayOrientation(Camera camera, int angle) {  
        Method downPolymorphic;  
        try {  
            downPolymorphic = camera.getClass().getMethod(  
                    "setDisplayOrientation", new Class[] { int.class });  
            if (downPolymorphic != null)  
                downPolymorphic.invoke(camera, new Object[] { angle });  
        } catch (Exception e1) {  
        }  
    } 
 <SurfaceView
     android:id="@+id/surfaceView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/> 
    <uses-permission android:name="android.permission.CAMERA" /><!-- 访问照像机的权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 允许挂载和移除文件系统可移动存储器 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 允许写扩展存储器 --> 

 

上一篇:Android Camera1-Camera2-CameraView和CameraX使用


下一篇:Android-Camera内存问题剖析,技术详细介绍