我有一个在后台拍照的服务...该服务在三种模式下工作:


前凸轮
后凸轮
带闪光灯的后凸轮


前两个可以很好地工作,但是,当启用Flash到Camera参数的闪光时,“ Camera.PictureCallback”中不会返回任何图像。
实际上,回调永远不会触发。

闪光灯闪光,看起来一切正常,但未收到图像。

这是我的onStartCommand:

public int onStartCommand(Intent intent, int flags, int startId) {

    try {
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_TOAST,
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                PixelFormat.TRANSLUCENT
        );

        params.gravity = Gravity.TOP | Gravity.START;
        params.width = 1;
        params.height = 1;
        params.x = 0;
        params.y = 0;
        sv = new SurfaceView(getApplicationContext());

        windowManager.addView(sv, params);
        SurfaceHolder sHolder = sv.getHolder();
        sHolder.addCallback(this);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return START_NOT_STICKY;
}


这是我的相机初始化代码:

                myCamera = Camera.open();

                if (myCamera != null) {
                    myCamera.setPreviewDisplay(sv.getHolder());
                    Camera.Parameters parameters = myCamera.getParameters();

                    parameters.setFlashMode(FLASH_MODE_ON);
                    parameters.setPictureFormat(ImageFormat.JPEG);
                    parameters.setJpegQuality(70);

                    // set camera parameters
                    myCamera.setParameters(parameters);

                    myCamera.startPreview();
                    myCamera.setErrorCallback(errorCallBack);
                    myCamera.takePicture(mShutter, null, mCall);
               }

最佳答案

我使用以下代码,并且可以在Samsung Galaxy 6、7上运行; LG Moto G4(我不记得该型号了)。

    public void onMyClick(View v){
        startTakingPictures();
    }


    private void startTakingPictures() {
        try {
            android.hardware.Camera cam = android.hardware.Camera.open();
            if (cam != null) {

                int presentOrientation  = getResources().getConfiguration().orientation;

                SurfaceView sv = new SurfaceView(getBaseContext());
                cam.setPreviewDisplay(sv.getHolder());
                Camera.Parameters para = cam.getParameters();

                para.setRotation(ORIENTATIONS.get(presentOrientation));
                para.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
//                Log.e(TAG, "lat lng == " + loc.getLatitude());
//                para.setGpsLatitude(loc.getLatitude());
//                para.setGpsLongitude(loc.getLongitude());
                cam.setParameters(para);

                cam.startPreview();
                cam.takePicture(null, null, getJpegCallBack());
            }

        }
        catch (Exception ex){
            Log.e(TAG, ex.getMessage());
        }
    }

    private Camera.PictureCallback getJpegCallBack() {
        Camera.PictureCallback jpg = new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                FileOutputStream fos;

                String path = getPathBaseName();
                if(path.isEmpty()){
                    return;
                }

                path = "image.jpg";
                Log.e(TAG, "Image path == " + path);

                try {
                    fos = new FileOutputStream(path);
                    fos.write(data);
                    fos.close();
                }
                catch (Exception ex) {
                    Log.e(TAG, ex.getMessage());
                }
            }
        };
        return jpg;
    }


编辑:

由于Camera现在已贬值,您可能要实现android.hardware.camera2

您的设备可以支持的参数类型似乎有问题。看一下可用的Flash参数:

Camera.Parameters para = cam.getParameters();
    for(String s : para.getSupportedFlashModes()){
        Log.e(TAG, "Supported Flash Mode =  " + s);
    }


您可能需要这样设置闪光模式:

param.set("flash-mode", "on");
cam.setParameters(param);


除此之外,您可能想在这篇文章中签出Karl Morrison的实现:
Android camera: stopPreview vs releaseCamera

关于android - 用闪光灯拍照不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46637382/

10-09 07:23