SimpleImageCaptureCommand.java revision 875d9fff24e283efa5d95ad75c3fab074e489fa4
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camera.one.v2.photo; 18 19import android.hardware.camera2.CameraAccessException; 20import android.hardware.camera2.CameraDevice; 21 22import com.android.camera.async.BufferQueue; 23import com.android.camera.async.Updatable; 24import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionClosedException; 25import com.android.camera.one.v2.camera2proxy.ImageProxy; 26import com.android.camera.one.v2.core.FrameServer; 27import com.android.camera.one.v2.core.RequestBuilder; 28import com.android.camera.one.v2.core.ResourceAcquisitionFailedException; 29import com.android.camera.one.v2.imagesaver.ImageSaver; 30import com.android.camera.one.v2.sharedimagereader.ImageStreamFactory; 31import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageStream; 32 33import java.util.Arrays; 34 35import javax.annotation.ParametersAreNonnullByDefault; 36 37import static com.android.camera.one.v2.core.ResponseListeners.forFrameExposure; 38import static com.android.camera.one.v2.core.ResponseListeners.forTimestamps; 39 40/** 41 * Captures single images. 42 */ 43@ParametersAreNonnullByDefault 44class SimpleImageCaptureCommand implements ImageCaptureCommand { 45 private final FrameServer mFrameServer; 46 private final RequestBuilder.Factory mBuilderFactory; 47 private final ImageStreamFactory mImageReader; 48 49 public SimpleImageCaptureCommand(FrameServer frameServer, RequestBuilder.Factory builder, 50 ImageStreamFactory imageReader) { 51 mFrameServer = frameServer; 52 mBuilderFactory = builder; 53 mImageReader = imageReader; 54 } 55 56 /** 57 * Sends a request to take a picture and blocks until it completes. 58 */ 59 @Override 60 public void run(Updatable<Void> imageExposureUpdatable, ImageSaver imageSaver) throws 61 InterruptedException, CameraAccessException, CameraCaptureSessionClosedException, 62 ResourceAcquisitionFailedException { 63 try (FrameServer.Session session = mFrameServer.createExclusiveSession(); 64 ImageStream imageStream = mImageReader.createStream(1)) { 65 RequestBuilder photoRequest = mBuilderFactory.create(CameraDevice 66 .TEMPLATE_STILL_CAPTURE); 67 photoRequest.addStream(imageStream); 68 photoRequest.addResponseListener(forFrameExposure(imageExposureUpdatable)); 69 session.submitRequest(Arrays.asList(photoRequest.build()), 70 FrameServer.RequestType.NON_REPEATING); 71 72 ImageProxy image = imageStream.getNext(); 73 imageSaver.addFullSizeImage(image); 74 } catch (BufferQueue.BufferQueueClosedException e) { 75 // If we get here, the request was submitted, but the image 76 // never arrived. 77 // TODO Log failure and notify the caller 78 } finally { 79 imageSaver.close(); 80 } 81 } 82} 83