Fotoapparat
介绍:
让Camera的api更简单,支持Camera1和 Camera2。
运行效果:
使用说明:
拍照就是如此简单:
Fotoapparat fotoapparat = Fotoapparat
.with(context)
.into(cameraView)
.build();
fotoapparat.start();
fotoapparat
.takePicture()
.saveToFile(someFile);
第一步
添加布局
<io.fotoapparat.view.CameraView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
第二步
配置 Fotoapparat instance
Fotoapparat
.with(context)
.into(cameraView) // view which will draw the camera preview
.photoSize(biggestSize()) // we want to have the biggest photo possible
.lensPosition(back()) // we want back camera
.focusMode(firstAvailable( // (optional) use the first focus mode which is supported by device
continuousFocus(),
autoFocus(), // in case if continuous focus is not available on device, auto focus will be used
fixed() // if even auto focus is not available - fixed focus mode will be used
))
.flash(firstAvailable( // (optional) similar to how it is done for focus mode, this time for flash
autoRedEye(),
autoFlash(),
torch()
))
.frameProcessor(myFrameProcessor) // (optional) receives each frame from preview stream
.logger(loggers( // (optional) we want to log camera events in 2 places at once
logcat(), // ... in logcat
fileLogger(this) // ... and to file
))
.build();
第三步
调用 start() 和 stop()
@Override
protected void onStart() {
super.onStart();
fotoapparat.start();
}
@Override
protected void onStop() {
super.onStop();
fotoapparat.stop();
}
拍照
最后是拍照,又多个选项
PhotoResult photoResult = fotoapparat.takePicture();
// Asynchronously saves photo to file
photoResult.saveToFile(someFile);
// Asynchronously converts photo to bitmap and returns result on main thread
photoResult
.toBitmap()
.whenAvailable(new PendingResult.Callback<BitmapPhoto>() {
@Override
public void onResult(BitmapPhoto result) {
ImageView imageView = (ImageView) findViewById(R.id.result);
imageView.setImageBitmap(result.bitmap);
imageView.setRotation(-result.rotationDegrees);
}
});
// Of course you can also get a photo in a blocking way. Do not do it on main thread though.
BitmapPhoto result = photoResult.toBitmap().await();
// Convert asynchronous events to RxJava 1.x/2.x types. See /fotoapparat-adapters/ module
photoResult
.toBitmap()
.adapt(SingleAdapter.<BitmapPhoto>toSingle())
.subscribe(bitmapPhoto -> {
});
依赖
Add dependency to your build.gradle
repositories {
maven { url 'https://jitpack.io' }
}
compile 'io.fotoapparat.fotoapparat:library:1.0.2'
已下载
0