Android手机如何自动拍照?

发布于 2024-12-02 01:11:35 字数 796 浏览 1 评论 0原文

我开发了一个android应用程序。我使用了前置摄像头功能。它工作正常,但我需要自动捕获。即没有点击快门按钮声音,我想捕获照片代码..

我的相机活动代码是

 private Camera openFrontFacingCameraGingerbread() { 
    int cameraCount = 0; 
    Camera cam = null; 
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
    cameraCount = Camera.getNumberOfCameras(); 
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) { 
        Camera.getCameraInfo( camIdx, cameraInfo ); 
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) { 
            try { 
                cam = Camera.open( camIdx ); 
            } catch (RuntimeException e) { 
                Log.i("Camera failed to open: ",e.getLocalizedMessage()); 
            } 
        } 
    } 
    return cam; 
} 

提前致谢..

I have developed an android application. In that i have used front facing camera functionality. Its working fine but I need to auto capture. i.e. without click shutter button sound, I want to capture photocode..

my camera activity code is

 private Camera openFrontFacingCameraGingerbread() { 
    int cameraCount = 0; 
    Camera cam = null; 
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
    cameraCount = Camera.getNumberOfCameras(); 
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) { 
        Camera.getCameraInfo( camIdx, cameraInfo ); 
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) { 
            try { 
                cam = Camera.open( camIdx ); 
            } catch (RuntimeException e) { 
                Log.i("Camera failed to open: ",e.getLocalizedMessage()); 
            } 
        } 
    } 
    return cam; 
} 

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朕就是辣么酷 2024-12-09 01:11:35

请按照 Android 开发人员参考页面中概述的步骤进行操作。不需要有“快门按钮”。如果您不想在屏幕上显示图像,可以创建一个虚拟 SurfaceHolder,例如

SurfaceView surface = new SurfaceView(context);
cam.setPreviewDisplay(surface.getHolder());

Follow the steps outlined in the Android Developer reference pages. There's no requirement to have a 'shutter button'. You can create a dummy SurfaceHolder if you don't want to show the image on the screen, e.g.

SurfaceView surface = new SurfaceView(context);
cam.setPreviewDisplay(surface.getHolder());
满意归宿 2024-12-09 01:11:35
    public int intPicTaken;


    // setPreviewCallback on the camera, wait intil intPicTaken increments to 10, then take the picture
    cam.setPreviewCallback(prevCallBack);

    public Camera.PreviewCallback prevCallBack = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            intPicTaken++;
            try {
                if(intPicTaken == 10) {
                doTakePicture();
                }
            } catch (Exception e) {
                System.out.println("onPreviewFrame: " + e.toString());
            }
        }
    };

    public Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            System.out.println("PictureCallback onPictureTaken");
            try {

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length, options);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                picture.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                baos.close();
                System.out.println("PictureCallback onPictureTaken done");
                cam.release();
                saveFile(picture);
            } catch (Exception e) {
                System.out.println("onPictureTaken: " + e.toString());
            }
        }
    };

    // take the picture
    public void doTakePicture() {
        try {

            cam.stopPreview();
            cam.takePicture(null, null, mPicture, mPicture);
        } catch(Exception e){
            System.out.println("doTakePicture: " + e.toString());
        }
    }

    // saving the file to gallery 
    public void saveFile(Bitmap bitmap) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaStorageDir = Environment.getExternalStorageDirectory();
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                System.out.println("saveFile: failed to create directory");
                return;
            }
        }
        try {
            String saved = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "title", "description");
            Uri sdCardUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, sdCardUri));
            System.out.println("file saved");
        } catch (Exception e) {
            System.out.println("saveFile: " + e.toString());
            e.printStackTrace();
        }
    }
    public int intPicTaken;


    // setPreviewCallback on the camera, wait intil intPicTaken increments to 10, then take the picture
    cam.setPreviewCallback(prevCallBack);

    public Camera.PreviewCallback prevCallBack = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            intPicTaken++;
            try {
                if(intPicTaken == 10) {
                doTakePicture();
                }
            } catch (Exception e) {
                System.out.println("onPreviewFrame: " + e.toString());
            }
        }
    };

    public Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            System.out.println("PictureCallback onPictureTaken");
            try {

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length, options);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                picture.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                baos.close();
                System.out.println("PictureCallback onPictureTaken done");
                cam.release();
                saveFile(picture);
            } catch (Exception e) {
                System.out.println("onPictureTaken: " + e.toString());
            }
        }
    };

    // take the picture
    public void doTakePicture() {
        try {

            cam.stopPreview();
            cam.takePicture(null, null, mPicture, mPicture);
        } catch(Exception e){
            System.out.println("doTakePicture: " + e.toString());
        }
    }

    // saving the file to gallery 
    public void saveFile(Bitmap bitmap) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaStorageDir = Environment.getExternalStorageDirectory();
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                System.out.println("saveFile: failed to create directory");
                return;
            }
        }
        try {
            String saved = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "title", "description");
            Uri sdCardUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, sdCardUri));
            System.out.println("file saved");
        } catch (Exception e) {
            System.out.println("saveFile: " + e.toString());
            e.printStackTrace();
        }
    }
半山落雨半山空 2024-12-09 01:11:35

在创建时写入此代码以自动捕获图像

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom__camera_activity);
    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mCameraPreview);


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

   {



                mCamera.takePicture(null, null, mPicture);

            }


    }
    }, 5500);
    }

write this code in on create to auto capture image

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom__camera_activity);
    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mCameraPreview);


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

   {



                mCamera.takePicture(null, null, mPicture);

            }


    }
    }, 5500);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文