Created
June 29, 2016 13:29
-
-
Save RoundSparrow/142b840ca86ba7a46639f23c5c0d195b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.Intent; | |
import android.graphics.ImageFormat; | |
import android.hardware.camera2.CameraAccessException; | |
import android.hardware.camera2.CameraCaptureSession; | |
import android.hardware.camera2.CameraCharacteristics; | |
import android.hardware.camera2.CameraDevice; | |
import android.hardware.camera2.CameraManager; | |
import android.hardware.camera2.CaptureRequest; | |
import android.media.Image; | |
import android.media.ImageReader; | |
import android.os.IBinder; | |
import android.support.annotation.NonNull; | |
import android.util.Log; | |
import java.util.Arrays; | |
/* | |
Reference: http://stackoverflow.com/questions/28003186/capture-picture-without-preview-using-camera2-api | |
Problem | |
1. BufferQueue has been abandoned from ImageCapture | |
*/ | |
public class SimpleCamera2ServicePublish extends Service { | |
protected static final String TAG = "VideoProcessing"; | |
protected static final int CAMERACHOICE = CameraCharacteristics.LENS_FACING_BACK; | |
protected CameraDevice cameraDevice; | |
protected CameraCaptureSession session; | |
protected ImageReader imageReader; | |
protected CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() { | |
@Override | |
public void onOpened(@NonNull CameraDevice camera) { | |
Log.i(TAG, "CameraDevice.StateCallback onOpened"); | |
cameraDevice = camera; | |
actOnReadyCameraDevice(); | |
} | |
@Override | |
public void onDisconnected(@NonNull CameraDevice camera) { | |
Log.w(TAG, "CameraDevice.StateCallback onDisconnected"); | |
} | |
@Override | |
public void onError(@NonNull CameraDevice camera, int error) { | |
Log.e(TAG, "CameraDevice.StateCallback onError " + error); | |
} | |
}; | |
protected CameraCaptureSession.StateCallback sessionStateCallback = new CameraCaptureSession.StateCallback() { | |
@Override | |
public void onConfigured(@NonNull CameraCaptureSession session) { | |
Log.i(TAG, "CameraCaptureSession.StateCallback onConfigured"); | |
SimpleCamera2ServicePublish.this.session = session; | |
try { | |
session.setRepeatingRequest(createCaptureRequest(), null, null); | |
} catch (CameraAccessException e){ | |
Log.e(TAG, e.getMessage()); | |
} | |
} | |
@Override | |
public void onConfigureFailed(@NonNull CameraCaptureSession session) {} | |
}; | |
protected ImageReader.OnImageAvailableListener onImageAvailableListener = new ImageReader.OnImageAvailableListener() { | |
@Override | |
public void onImageAvailable(ImageReader reader) { | |
Log.i(TAG, "onImageAvailable"); | |
Image img = reader.acquireLatestImage(); | |
if (img != null) { | |
processImage(img); | |
img.close(); | |
} | |
} | |
}; | |
public void readyCamera() | |
{ | |
CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE); | |
try { | |
String pickedCamera = getCamera(manager); | |
manager.openCamera(pickedCamera, cameraStateCallback, null); | |
imageReader = ImageReader.newInstance(320, 240, ImageFormat.YUV_420_888, 2 /* images buffered */); | |
imageReader.setOnImageAvailableListener(onImageAvailableListener, null); | |
Log.i(TAG, "imageReader created"); | |
} catch (CameraAccessException e){ | |
Log.e(TAG, e.getMessage()); | |
} | |
} | |
/** | |
* Return the Camera Id which matches the field CAMERACHOICE. | |
*/ | |
public String getCamera(CameraManager manager){ | |
try { | |
for (String cameraId : manager.getCameraIdList()) { | |
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); | |
int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING); | |
if (cOrientation == CAMERACHOICE) { | |
return cameraId; | |
} | |
} | |
} catch (CameraAccessException e){ | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.i(TAG, "onStartCommand flags " + flags + " startId " + startId); | |
readyCamera(); | |
return super.onStartCommand(intent, flags, startId); | |
} | |
public void actOnReadyCameraDevice() | |
{ | |
try { | |
cameraDevice.createCaptureSession(Arrays.asList(imageReader.getSurface()), sessionStateCallback, null); | |
} catch (CameraAccessException e){ | |
Log.e(TAG, e.getMessage()); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
try { | |
session.abortCaptures(); | |
} catch (CameraAccessException e){ | |
Log.e(TAG, e.getMessage()); | |
} | |
session.close(); | |
} | |
/** | |
* Process image data as desired. | |
*/ | |
private void processImage(Image image){ | |
//Process image data | |
} | |
protected CaptureRequest createCaptureRequest() { | |
try { | |
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); | |
builder.addTarget(imageReader.getSurface()); | |
return builder.build(); | |
} catch (CameraAccessException e) { | |
Log.e(TAG, e.getMessage()); | |
return null; | |
} | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
} |
Look into focusing the camera before shooting @shaunaa126
How do I fix the code so that the photos are the right exposure? The pictures are either very dark or very light! HELP!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello RoundSparrow. Thanks for creating this service I have been looking for this code for some time.
I have a quick question... when I start the service it results in a picture that is broken or black. I do not think it is providing any time to open the camera and establish itself. Any idea on a solution for this?