使用IntentService用Camera2拍照 [英] Take a photo with Camera2 using IntentService

查看:79
本文介绍了使用IntentService用Camera2拍照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在不显示预览的情况下拍照的应用程序.使用本教程- https://www.youtube.com/watch?v=oPu42I0HSi4如果使用和 Activity ,我设法使其正常运行.现在,我尝试将处理图片操作的代码移到 IntentService 中.

I am trying to create an application which takes a photo without showing a preview. Using this tutorial- https://www.youtube.com/watch?v=oPu42I0HSi4 I managed to make it work if I use and Activity. Now I am trying to move the code that handles the picture-taking action into and IntentService.

这就是我到目前为止所得到的:

and this is what I got so far:

MainActivity.java-活动

private void startCameraService()
{
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this,new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        },REQUEST_CAMERA_PERMISSION);
        return;
    }

    Intent cameraService = new Intent(this, CameraService.class);
    startService(cameraService);
}

CameraService.java-(意图服务)

public class CameraService extends IntentService {

//Input image state
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static{
    ORIENTATIONS.append(Surface.ROTATION_90,90);
    ORIENTATIONS.append(Surface.ROTATION_0,0);
    ORIENTATIONS.append(Surface.ROTATION_180,270);
    ORIENTATIONS.append(Surface.ROTATION_270,180);
}

//Camera variables
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSessions;
private CaptureRequest.Builder captureRequestBuilder;
private Size imageDimension;
private ImageReader imageReader;

//Output file variables
private File file;
private static final int REQUEST_CAMERA_PERMISSION = 200;
private boolean mFlashSupported;
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;

//the orientation of the device
int rotation;
//bitmap of the taken photo
private Bitmap bitmapImage;

//camera state callback
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(@NonNull CameraDevice camera) {
        cameraDevice = camera;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onDisconnected(@NonNull CameraDevice cameraDevice) {
        cameraDevice.close();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onError(@NonNull CameraDevice cameraDevice, int i) {
        cameraDevice.close();
        cameraDevice=null;
    }
};


public CameraService() {
    super("CameraService");
}

@Override
protected void onHandleIntent(Intent intent)
{
    takePicture();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void takePicture() {
    Log.d("CameraService","inside TakePicture");
    if(cameraDevice == null)
    {
        return;
    }
    CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
    try{
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
        Size[] jpegSizes = null;
        if(characteristics != null)
            jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
                    .getOutputSizes(ImageFormat.JPEG);

        //Capture image with custom size
        int width = 640;
        int height = 480;
        if(jpegSizes != null && jpegSizes.length > 0)
        {
            width = jpegSizes[0].getWidth();
            height = jpegSizes[0].getHeight();
        }
        final ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
        List<Surface> outputSurface = new ArrayList<>(2);
        outputSurface.add(reader.getSurface());

        final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(reader.getSurface());
        captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);

        File file = new File(Environment.getExternalStorageDirectory() + "/"+UUID.randomUUID().toString()+".jpg");

        ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader imageReader) {
                Image image = null;
                try{
                    image = reader.acquireLatestImage();

                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                    save(bytes);
                    bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);

                    //TODO: fix the orientation so despite the phone position the photo is always correct and not rotated
                    // use  ORIENTATIONS.get(rotation)
                    bitmapImage = rotateImage(bitmapImage, 270);
                }
                catch (Exeption e)
                {
                    e.printStackTrace();
                }
                finally {
                    {
                        if(image != null)
                            image.close();
                    }
                }
            }
            private void save(byte[] bytes) throws IOException {
                OutputStream outputStream = null;
                try{
                    outputStream = new FileOutputStream(file);
                    outputStream.write(bytes);
                }finally {
                    if(outputStream != null)
                        outputStream.close();
                }
            }
        };

        reader.setOnImageAvailableListener(readerListener,mBackgroundHandler);
        final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);
                Log.d("CameraService","image is saved");
            }
        };

        cameraDevice.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                try{
                    cameraCaptureSession.capture(captureBuilder.build(),captureListener,mBackgroundHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {

            }
        },mBackgroundHandler);


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

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
            matrix, true);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void openCamera() {
    CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
    try{
        cameraId = manager.getCameraIdList()[1];
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
        StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        assert map != null;
        imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];

        manager.openCamera(cameraId,stateCallback,null);

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

我的问题是,当我调用该服务时, cameraDevice .应该在这里填充:

My problem is that, when I call the service the cameraDevice is null. It should be populated here:

//camera state callback
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(@NonNull CameraDevice camera) {
        cameraDevice = camera;
    }

CameraDevice相机为空.

有什么建议为什么它可以作为 Activity 而不是 IntentService ?如果您能帮助我,我将不胜感激

Any suggestions why this is working as an Activity and not as an IntentService? I would much appreciate it if you could help me

推荐答案

调用onHandleIntent()后,将调用stopSelf().因此,服务会在发生摄像头事件时停止.我想您必须以某种方式使Looper.loop()在您的onHandleIntent()内运行.

After calling onHandleIntent(), stopSelf() is called. So service is stopped by the time camera events come. I guess you have to somehow keep the Looper.loop() running within your onHandleIntent().

这篇关于使用IntentService用Camera2拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆