Android studio camera github
Lightweight Library for integrating the Camera sensor on your Android apps. The library internally takes care of invoking the correct API — the deprecated android.hardware.Camera or the new android.hardware.Camera2 . You get a generated image in the bitmap format which you can save (as JPEGs etc) or render in your apps. The returned bitmap is guaranteed to have the correct orientation and resolution.
In addition to stability and various bug fixes, this new API has the following additional features:
- You can now choose a TextureView or SurfaceView as your preview surface. Deafult is the more performant SurfaceView . The options are CameraAPI.PreviewType.TEXTURE_VIEW and CameraAPI.PreviewType.SURFACE_VIEW which can be set in the CameraAPIClient. See example below.
- You can add a desired aspect ratio as input parameters. Different cameras support different aspect ratio. So if your camera does not supper the desired aspect ratio — it chooses one by default. Additionally, in a callback onAspectRatioAvailable you get to know all possible size/ aspect ratio choices for your front and back cameras. Note that these are likely to be different. After you are aware of the supported sizes, in the next invocation, you can choose one of the available sizes.
- You can also set the maximum size of the smaller dimension of the JPEG image. The smaller dimension is typically the top of the phone if you hold it in portrait mode. The resultant bitmap will have max that size, without disturbing the aspect ratio.
- Back button navigation should work.
Unfortunately, all this needed a major API change. We now have a CameraAPIClient See the MainActivity for detailed examples.
Start the camera by
where Callback is an instance of CameraAPI.Callback
Add the library dependency to your build.gradle .
The main entry point to this library is the singleton CameraController class. You can use the getInstance() method to get an instance object of the class. Start the camera using the launch method of this class which takes three arguments:
- As the first arguement, pass the Activity from which this is launched.
- The Camera is launched inside a Fragment . You will have to define a place in your layout file to launch the Fragment . The ID of this node in the layout file is the second argument. For instance, in the layout file within the demo app, we define a FrameLayout where the camera will be displayed.
- The third argument is a callback object which notifies the caller about various lifecycle events of the Camera . It is an instance of the interface CAmeraController.Callback and you can add your own code in the object about what you wich to do when these lifecycle events are triggered.
Checkout the example app built using this library app/src/main/java/xyz/pinaki/androidcamera/example
Why This Library
There are multiple issues with the camera libraries on Android.
The new Camera2 library is only supported by Android Lollipop (21) and higher. However, I have found in many post 21 devices, the camera sensor does not behave well when used with Camera2 library. In some cases, even if one of the cameras support it — the other might not. The CameraCharacteristics store the hardware level support. This library includes the logic to choose the correct library.
During my search, I was not able to find a lightweight library, which enables developer capture an image from either of the cameras with correct orientation and aspect ratios. Hence, I created this library.
Based on how the camera sensor is mounted on the device, we have to correct the preview image as well as the final bitmap. Else the orientation will be incorrect. Furthermore, based on how the user is holding the camera (portrait or landscape), the final image needs to be corrected. This library takes care of those scenarios.
This library enables easily switch between front and rear cameras. The images from the front camera are corrected so that they show up as mirror reflections.
Handling the camera callbacks correctly in background thread without blocking the UI thread.
Correct Aspect Ratio for the camera (the cameras only support some aspect ratios), the preview image (which are constrained by the display dimensions) and the final image.
The above points are explained in details below. To summarize, the goal behind this library is to let app developers integrate camera in their apps and take a picture (with either of the front and rear facing cameras) without delving too much into the details of how the camera libraries are implemented. The final bitmap has the correct aspect ration and orientation so they can either be save or uploaded to remote hosts like AWS S3.
Источник
Android studio camera github
Failed to load latest commit information.
README.md
For our app TeamUp, we needed complete control over the camera, due to a highly demanding privacy requirement. An implementation on top of the cwac-camera library, created by Mark Murphy.
You can also read the blog post, with a more tutorial like example.
To add this lib as a dependency:
- Pull this repository to tyou local machine git clone https://github.com/askcs/android-simple-camera
- Install the project into your local repository (assuming you have a repository) gradlew install . The project is build and uploaded to your local repository.
- Use the project as a dependency
This implementation is built around the CameraFragment . The CameraFragment is wrapped in an Activity, together with a Builder class, that only reveals a couple of the options of the SimpleCameraHost. This is done to keep it simple.
Request Camera and Mirophone Permissions
If your project is targeting API 23+ or will be used on such devices, you have to request these permissions from the user.
Before using the library, make sure you have the appropriate permissions. You can do this anywhere in your application before accessing the camera.
You should also check whether the user has consented to the request. See https://developer.android.com/training/permissions/requesting.html for further details.
###Using the Builder class The following snippet creates a new SimpleCameraActivity.Buidler object, chooses the back facing camera, selects the Size.AVATAR and gives no location. If no location is given, the picture will be saved in the cache folder of your app.
####Things to configure Call these methods on the SimpleCameraActivity.Builder to configure.
frontFacingCamera(boolean) Set to true to use the front facing camera. If the front facing camera is not available on the device, it will fall back on the back facing camera. Default false .
dir(File) Sets the directory where the picture/video should be saved. Default: context.getCacheDir() .
filename(String) Sets the filename. Make sure NOT to include the file extension! A picture will always be JPEG, a video will always be MP4. Default: UUID.randomUUID().toString()
size(SimpleCameraFragment.Size) Sets the size of the picture that will be saved. The preview of the SimpleCameraFragment will be adjusted to this size. Default: Size.NORMAL
When done, build(Context) to get the Intent, or startForResult(Activity, int) to directly start the SimpleCameraActivity.
Also, a build(Context, Class) and startForResult(Activity, Class, int) are available, whenever you subclass SimpleCameraActivity.
####Example MyActivity.java
###Or via an Intent The following snippet creates a new Intent for SimpleCameraActivity . It chooses the back facing camera, selects the Size.NORMAL and gives the app cache directory as directory location and an earlier set filename.
#####Things to configure Include these extras in the Intent to configure the SimpleCameraActivity . You could also use the Builder to configure it, and than call .build(Context) on it to get the Intent.
EXTRA_START_WITH_FRONT_FACING_CAMERA boolean Set to true to use the front facing camera. If the front facing camera is not available on the device, it will fall back on the back facing camera. Default false .
EXTRA_DIR String Sets the directory where the picture/video should be saved. Default : context.getCacheDir() .
EXTRA_FILENAME String The filename. Make sure NOT to include the file extension! A picture will always be JPEG, a video will always be MP4. Default: UUID.randomUUID().toString()
EXTRA_SIZE int Sets the size of the picture that will be saved. Use the .ordinal() of a SimpleCameraFragment.Size . If the found integer is not known as an ordinal of Size , the default is used. The preview of the SimpleCameraFragment will be adjusted to this size. Default: Size.NORMAL
####Example MyActivity.java
###Handle the result in onActivityResult()
MyActivity.java
- A picture is always saved with a .jpg suffix
- A video is always saved with a .mp4 suffix
- The cwac-camera takes care of rotating the pictures the right way
- Recording videos and switching the camera is currently disabled
The following attributes are available in the SimpleCamera theme:
- scButtonSwitchCameraStyle : Allows the user to switch front/back facing camera. Only when multiple cameras are available
- scButtonTakePictureStyle : Button to take the actual picture
- scButtonRecordVideoStyle : Button to start recording a video
- scButtonStopRecordingStyle : Button to stop recording a video. Visible after record button is clicked
- scButtonAcceptStyle : Button to accept the picture / video after it is taken
- scButtonRejectStyle : Button to reject the picture / video after it is taken
Don’t forget to set the theme in your manifest:
manifest.xml This is a part of the manifest. See the snippet at the top of the blog article for the complete manifest.
The following should be locally installed:
Get Up and Running
The Android Support Library v13 and Google Maps v2 are needed by the TeamUp App, which are not in the central Maven repository. You need to install them using Android’s SDK manager, and then use the maven-android-sdk-deployer to put these libraries in your local Maven repository.
Install Android Support Library v13
Start the SDK manager by doing:
and install/update the Android Support Library v13 (it’s located under Extras), and install everything under SDK API level 17.
And then install it to your local Maven repository:
###Clone this project
###Create local.properties Inside the root folder, android-simple-camera/ , create a file called local.properties and add a single entry to it:
###Install And install the library into your local maven repository
##Using in your project
In your POM, add this library as a dependency. Note to add it as an apklib .
Источник
Android studio camera github
ULTIMATE ANDROID CAMERA GUIDE
The official «Ultimate Android Camera Guide» source repo.
Includes examples on how to..
- Use android’s native camera to capture an image
- Use android’s external camera to capture an image
- Load images from the gallery into a grid view
- Use Androids gallery to «Select» a single image
- Display images in a horizontal list form
Download / clone. Open in Android Studio. Run. The end.
This project makes use of source code snippets provided by Google’s Android development portal and TwoWayView by Lucas Rocha: https://github.com/lucasr/twoway-view. Certain code portions are derived partially from results found on StackOverflow during research:
Copyright (c) 2014 Rex St John, @rexstjohn
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the «Software»), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
About
The official «Ultimate Android Camera Guide» source repo
Источник