RxJava style camera API for android, it based on android.hardware.camera
Add to your project dependence:
repositories {
jcenter()
}
dependencies {
compile 'com.ragnarok.rxcamera:lib:0.0.2'
}
Usage:
-
set the camera parameter by choose a RxCameraConfig, which created by RxCameraConfigChooser:
RxCameraConfig config = RxCameraConfigChooser.obtain(). useBackCamera(). setAutoFocus(true). setPreferPreviewFrameRate(15, 30). setPreferPreviewSize(new Point(640, 480)). setHandleSurfaceEvent(true). get();
for all camera currently support, please see RxCameraConfig
-
open camera
RxCamera.open(context, config)
it return an RxJava Observable object, the type is
Observable<RxCamera>
-
bind a
SurfaceView
orTextureView
and startPreviewsince
RxCamera.open
is return an Observable, so you can chain the call like thisRxCamera.open(this, config).flatMap(new Func1<RxCamera, Observable<RxCamera>>() { @Override public Observable<RxCamera> call(RxCamera rxCamera) { return rxCamera.bindTexture(textureView); // or bind a SurfaceView: // rxCamera.bindSurface(SurfaceView) } }).flatMap(new Func1<RxCamera, Observable<RxCamera>>() { @Override public Observable<RxCamera> call(RxCamera rxCamera) { return rxCamera.startPreview(); } });
both
RxCamera.bindTexture
andRxCamera.startPreview
will return anObservable<RxCamera>
objectPS: if set
isHandleSurfaceEvent
to true(set bysetHandleSurfaceEvent(true)
inRxCameraConfigChooser
), RxCamera will do the actual camera start preview action when the surface is available, otherwise it will start preview immediately, and may failed if surface is not available, in this case, the return Observable will callonError
-
request camera data
RxCamera support many styles of camera data requests:
-
successiveDataRequest
camera.request().successiveDataRequest()
it will return the camera data infinitely
-
periodicDataRequest
camera.request().periodicDataRequest(1000)
as the name, it will return camera data periodic, pass the interval in millisecond
-
oneShotRequest
camera.request().oneShotRequest()
it will return the camera data only once
-
takePictureRequest
camera.request().takePictureRequest(boolean isContinuePreview, Func shutterAction)
the encapsulation of takePicture API, if
isContinuePreview
set to true, the RxCamera will try to restart preview after capture the picture, otherwise will behave as systemtakePicture
call, stop preview after captured successfullyand the
shutterAction
will called after picture just captured, like the [ShutterCallback] (http://developer.android.com/intl/es/reference/android/hardware/Camera.ShutterCallback.html) (actually it is called in the system shutterCallback)
all the data request will return an
Observalbe<RxCameraData>
the
RxCameraData
contained two fields:byte[] cameraData
, the raw data of camera, for the takePicture request, it will return the jpeg encode byte, other request just return raw camera preview data, if you don't set preview format, the default is YUV420SPMatrix rotateMatrix
, this matrix help you rotate the camera data in portrait
-
This project still in very early stage, and welcome the pull request