使用 IntentService 使用 Camera2 拍照

Posted

技术标签:

【中文标题】使用 IntentService 使用 Camera2 拍照【英文标题】:Take a photo with Camera2 using IntentService 【发布时间】:2018-07-23 22:23:09 【问题描述】:

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

这是我目前得到的:

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();
    


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

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

CameraDevice 相机 为空。

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

【问题讨论】:

您找到解决方案了吗?我正在寻找相同的方法,将 camera2 作为 intentService 运行 【参考方案1】:

调用onHandleIntent() 后,调用stopSelf()。因此,当相机事件到来时,服务就会停止。我猜你必须以某种方式让Looper.loop() 在你的onHandleIntent() 中运行。

【讨论】:

以上是关于使用 IntentService 使用 Camera2 拍照的主要内容,如果未能解决你的问题,请参考以下文章

IntentService的使用

Android Training - 使用IntentService运行任务(Lesson 2 - 发送任务给IntentService)

IntentService 源码分析

如何使用 Intentservice 获取位置信息?

Android中使用IntentService运行后台任务

Android中使用IntentService运行后台任务