android 拍照

该博客围绕 Android 拍照展开,虽无具体内容,但可知聚焦于 Android 系统下的拍照功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    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" /><!-- 允许写扩展存储器 --> 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值