4

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

请帮助我一步一步地捕获图片并保存到 SD 卡。

4

1 回答 1

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;
                }
            }

另外,请查看此文档

于 2012-09-09T17:23:09.203 回答