Android:是否可以使用没有UI的服务从相机拍照

时间:2013-01-11 12:13:28

标签: android camera

我能够使用相机拍摄照片的唯一代码来自一项活动。我相当确定可以从服务中或从服务启动的AsyncTask中获取照片。

在我看来,相机API需要一个必须绑定到UI的SurfaceView。也许我错了。有人写过代码可以从服务中获取照片吗?

3 个答案:

答案 0 :(得分:5)

可以在Android中使用WindowManager

https://stackoverflow.com/a/10268650/3047840

我应该说是的,您需要SurfaceView来拍照,但不一定要绑定到某个xml布局。您可以将其添加到WindowManager类中,该类甚至可以在服务中使用。因此,此处的WindowManager会打开一个“窗口”供您在Android中执行任何浮动效果。

具体来说,

您可以在SurfaceView中添加WindowManager并将参数设置如下。

mPreview = new CameraPreview(this, mCamera, jpegCallback);
WindowManager wm = (WindowManager) this
        .getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
        PixelFormat.TRANSPARENT);

params.height = 1;
params.width = 1;

wm.addView(mPreview, params);

Android具有此功能,而iOS则没有。这就是为什么你可以在Android主屏幕而不是iOS上激活Facebook聊天。

Show facebook like chat head from a broadcast receiver in android

答案 1 :(得分:2)

如果你还在寻找,这里有一些更完整的代码。在服务中工作(测试):

private void takePhoto() {

    System.out.println( "Preparing to take photo");
    Camera camera = null;

    int cameraCount = 0;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        SystemClock.sleep(1000);

        Camera.getCameraInfo(camIdx, cameraInfo);

            try {
                camera = Camera.open(camIdx);
            } catch (RuntimeException e) {
                System.out.println("Camera not available: " + camIdx);
                camera = null;
                //e.printStackTrace();
            }
        try{
            if (null == camera) {
                System.out.println("Could not get camera instance");
            }else{
                System.out.println("Got the camera, creating the dummy surface texture");
                //SurfaceTexture dummySurfaceTextureF = new SurfaceTexture(0);
                try {
                    //camera.setPreviewTexture(dummySurfaceTextureF);
                    camera.setPreviewTexture(new SurfaceTexture(0));
                    camera.startPreview();
                } catch (Exception e) {
                    System.out.println("Could not set the surface preview texture");
                    e.printStackTrace();
                }
                camera.takePicture(null, null, new Camera.PictureCallback() {

                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        File pictureFileDir = getDir();
                        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
                            return;
                        }
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
                        String date = dateFormat.format(new Date());
                        String photoFile = "PictureFront_" + "_" + date + ".jpg";
                        String filename = pictureFileDir.getPath() + File.separator + photoFile;
                        File mainPicture = new File(filename);
                        addImageFile(mainPicture);

                        try {
                            FileOutputStream fos = new FileOutputStream(mainPicture);
                            fos.write(data);
                            fos.close();
                            System.out.println("image saved");
                        } catch (Exception error) {
                            System.out.println("Image could not be saved");
                        }
                        camera.release();
                    }
                });
            }
        }catch (Exception e){
            camera.release();
        }


    }

答案 2 :(得分:-2)

我不认为这是可能的,因为相机需要预览屏幕。请参阅类似问题here