4

可能重复:
使用前置摄像头拍摄图像而无需在 android 中打开摄像头应用程序

我的问题是我只能通过使用 Intent 启动相机并单击按钮来捕获图像来进行捕获。是否可以通过不单击按钮来自动执行此操作,或者我可以添加哪些代码来执行此操作?提前致谢。

4

2 回答 2

8
public class CameraView extends Activity implements SurfaceHolder.Callback, OnClickListener{
        private static final String TAG = "CameraTest";
        Camera mCamera;
        boolean mPreviewRunning = false;

        @SuppressWarnings("deprecation")
        public void onCreate(Bundle icicle){
            super.onCreate(icicle);
            Log.e(TAG, "onCreate");

            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.cameraview);
            ImageView img = (ImageView) findViewById(R.id.blankImage);

            if(CaptureCameraImage.isBlack)
                img.setBackgroundResource(android.R.color.black);
            else
                img.setBackgroundResource(android.R.color.white);

            mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
            mSurfaceView.setOnClickListener(this);
            mSurfaceHolder = mSurfaceView.getHolder();
            mSurfaceHolder.addCallback(this);
            mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        }

        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState){
            super.onRestoreInstanceState(savedInstanceState);
        }


        Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

            public void onPictureTaken(byte[] data, Camera camera) {
                // TODO Auto-generated method stub
                if (data != null){
                    //Intent mIntent = new Intent();
                    //mIntent.putExtra("image",imageData);

                    mCamera.stopPreview();
                    mPreviewRunning = false;
                    mCamera.release();

                     try{
                         BitmapFactory.Options opts = new BitmapFactory.Options();
                         Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length,opts);
                         bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
                         int width = bitmap.getWidth();
                         int height = bitmap.getHeight();
                         int newWidth = 300;
                         int newHeight = 300;

                         // calculate the scale - in this case = 0.4f
                         float scaleWidth = ((float) newWidth) / width;
                         float scaleHeight = ((float) newHeight) / height;

                         // createa matrix for the manipulation
                         Matrix matrix = new Matrix();
                         // resize the bit map
                         matrix.postScale(scaleWidth, scaleHeight);
                         // rotate the Bitmap
                         matrix.postRotate(-90);
                         Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                                 width, height, matrix, true);
                         CaptureCameraImage.image.setImageBitmap(resizedBitmap);

                     }catch(Exception e){
                         e.printStackTrace();
                     }
                    //StoreByteImage(mContext, imageData, 50,"ImageName");
                    //setResult(FOTO_MODE, mIntent);
                    setResult(585);
                    finish();
                }       
            }
        };

        protected void onResume(){
            Log.e(TAG, "onResume");
            super.onResume();
        }

        protected void onSaveInstanceState(Bundle outState){
            super.onSaveInstanceState(outState);
        }

        protected void onStop(){
            Log.e(TAG, "onStop");
            super.onStop();
        }

        @TargetApi(9)
        public void surfaceCreated(SurfaceHolder holder){
            Log.e(TAG, "surfaceCreated");
            mCamera = Camera.open(CaptureCameraImage.cameraID);
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            Log.e(TAG, "surfaceChanged");

            // XXX stopPreview() will crash if preview is not running
            if (mPreviewRunning){
                mCamera.stopPreview();
            }

            Camera.Parameters p = mCamera.getParameters();
            p.setPreviewSize(300, 300);

            if(CaptureCameraImage.cameraID == 0){
                String stringFlashMode = p.getFlashMode();
                if (stringFlashMode.equals("torch"))
                        p.setFlashMode("on"); // Light is set off, flash is set to normal 'on' mode
                else
                        p.setFlashMode("torch");
            }

            mCamera.setParameters(p);
            try{
                mCamera.setPreviewDisplay(holder);
            }catch (Exception e){
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
            mCamera.takePicture(null, mPictureCallback, mPictureCallback);
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.e(TAG, "surfaceDestroyed");
            //mCamera.stopPreview();
            //mPreviewRunning = false;
            //mCamera.release();
        }

        private SurfaceView mSurfaceView;
        private SurfaceHolder mSurfaceHolder;

        public void onClick(View v) {
            // TODO Auto-generated method stub
            mCamera.takePicture(null, mPictureCallback, mPictureCallback);
        }

    }

您需要调用此活动还检查下面的代码行 mCamera.takePicture(null, mPictureCallback, mPictureCallback); 它被调用了两次 1)如果您希望用户交互拍照,您需要提交第一次出现 2)如果您不希望用户交互(一开始就会拍照)您可以在以下位置找到整个项目

https://github.com/sandipmjadhav/CaptureImage

谢谢你

于 2012-09-07T11:38:56.707 回答
0

是的,您无需按下拍摄按钮即可自动拍摄图像。您要捕获图像的 Activity 应该extends Activityimplements PictureCallback, ShutterCallback.

您必须使用以下方法

videoPreview.getCamera().takePicture(this, null, this);

videoPreview是我创建的类的一个实例,它extends SurfaceView实现了SurfaceHolder.Callback

Camera.open();也在VideoPriview类中

于 2012-09-07T09:58:55.570 回答