Fotoapparat让 Android 相机更友好
Fotoapparat
 
 
让 Camera 的 API 更简单,支持 Camera1 和 Camera2 。
让拍照变得更简单:
Fotoapparat fotoapparat = Fotoapparat .with(context) .into(cameraView) .build(); fotoapparat.start(); fotoapparat .takePicture() .saveToFile(someFile);
如何运行
第一步:将 CameraView 添加到布局
<io.fotoapparat.view.CameraView android:id="@+id/camera_view" android:layout_width="match_parent" android:layout_height="match_parent"/>
第二步:配置 Fotoapparat 实例
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 -> {
            
        });评论
