如何使用前置摄像头逐步拍摄照片?

时间:2012-09-08 12:59:57

标签: android

如何捕捉前置摄像头上的图片,我正在处理短信事件,当我的手机收到一些带有代码的短信时,我的手机会自动捕捉图片并将其保存到SD卡。

请一步一步帮助我拍摄照片并保存到SD卡。

1 个答案:

答案 0 :(得分:2)

使用以下代码拍照:

                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                String newPicFile = "Image" + position;
                String outPath = "/sdcard/" + newPicFile;
                File outFile = new File(outPath);
                mCameraFileName = outFile.toString();
                Uri outuri = Uri.fromFile(outFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
                try{
                    startActivityForResult(intent, 1);
                }
                catch (ActivityNotFoundException e) 
                {
                    Toast.makeText(context, "No Camera Found", Toast.LENGTH_LONG).show();
                }

此代码可查看点击的图片:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if( requestCode == CAMERA_PIC_REQUEST)
        {
        //  data.getExtras()
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
            image.setImageBitmap(thumbnail);
        }
        else 
        {
            Toast.makeText(context, "Picture Not taken", Toast.LENGTH_LONG).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

要检查设备中的前置摄像头,可以使用以下代码:

CameraInfo cameraInfo = new CameraInfo();
            for (int i = 0; i < numberOfCameras; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                    defaultCameraId = i;
                }
            }

另外,请看一下这个documentation