Fotoapparat


Source link: https://github.com/Fotoapparat/Fotoapparat

Fotoapparat

Camera API in Android is hard. Having 2 different API for new and old Camera does not make things any easier. But fret not, that is your lucky day! After several years of working with Camera we came up with Fotoapparat.

What it provides:

  • Camera API which does not allow you to shoot yourself in the foot.
  • Simple yet powerful parameters customization.
  • Standalone custom CameraView which can be integrated into any Activity.
  • Fixes and workarounds for device-specific problems.
  • Last, but not least, non 0% test coverage.

Taking picture becomes as simple as:

Fotoapparat fotoapparat = Fotoapparat
  .with(context)

 .into(cameraView)
  .build();
  fotoapparat.start();

fotoapparat
  .takePicture()
  .saveToFile(someFile);

How it works

Step One

Add CameraView to your layout

<io.fotoapparat.view.CameraView
  android:id="@+id/camera_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

Step Two

Configure Fotoapparat instance

Fotoapparat
  .with(context)

 .into(cameraView)

  // view which will draw the camera preview
  .previewScaleType(ScaleType.CENTER_CROP)  // we want the preview to fill the view  
  .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();

Step Three

Call start() and stop(). No rocket science here.

@Override protected void onStart() {

  super.onStart();

  fotoapparat.start();
 
}
  @Override protected void onStop() {

  super.onStop();

  fotoapparat.stop();
 
}

Take picture

Finally we are ready to take picture. You have various options.

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 -> {

 
}
);

Update parameters

It is also possible to update some parameters after Fotoapparat was already started.

fotoapparat.updateParameters(

UpdateRequest.builder()

  .flash(

isTurnedOn

  ? torch()

  : off()

  )

  .build() )

Set up

Add dependency to your build.gradle

repositories {

maven {
 url 'https://jitpack.io' 
}
 
}

compile 'io.fotoapparat.fotoapparat:library:1.4.1'

Camera permission will be automatically added to your AndroidManifest.xml. Do not forget to request this permission on Marshmallow and higher.

Face detection

Optionally, you can check out our other library which adds face detection capabilities - FaceDetector.

Credits

We want to say thanks to Mark Murphy for the awesome job he did with CWAC-Camera. We were using his library for a couple of years and now we feel that Fotoapparat is a next step in the right direction.

We also want to say many thanks to Leander Lenzing for the amazing icon. Don't forget to follow his work in dribbble.

License

Copyright 2017 Fotoapparat  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

MaterialDialogSearchView is a custom implementation of a Toolbar SearchView in a material design. It is an alternate approach using Material dialogs as SearchView similar to the applications seen on nowadays with this type of SearchView's in the apps like Google play, Google card's, etc.

Android lifecycle safety task with sugar code style.

Collect UncaughtException when your Android App crash.

A floating action buttons which are transformed into toolbar.

A gradle plugin that helps developer migrate to code with Android Studio + Gradle, but build && install with buck.

This is a simple library that provides some unique components for aiding in making apps comply with the material design guidelines.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes