📸 Use Android camera to take pictures and videos, based on `camera2` api.

Overview

Camera gitHub release platform Android Arsenal license Build status

Use Android camera to take pictures and videos, based on camera2 api.

Features

  • Auto filled CameraView for previewing
  • Support both image capture & video record
  • Configurable audio/video size and aspect ratio, auto focus, tap to focus, flash control, pinch to zoom, etc.

Gradle

dependencies {
    implementation 'com.github.duanhong169:camera:${latestVersion}'
    ...
}

Replace ${latestVersion} with the latest version code. See releases.

Usage

  • Add CameraView into your layout xml:
<top.defaults.camera.CameraView
    android:id="@+id/preview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    app:mode="video"
    app:aspectRatio="4:3"
    app:autoFocus="true"
    app:facing="back"
    app:fillSpace="false"
    app:flash="auto"
    app:pinchToZoom="false"
    app:showFocusIndicator="true"/>

See top_defaults_camera_attrs.xml for all supported attributes.

  • Create a Photographer with the CameraView:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // ...

    CameraView preview = findViewById(R.id.preview);
    photographer = PhotographerFactory.createPhotographerWithCamera2(this, preview);
    
    // ...
}
  • Implement and set Photographer.OnEventListener to receive events from the camera:
    photographer.setOnEventListener(new SimpleOnEventListener() {
        @Override
        public void onDeviceConfigured() {}

        @Override
        public void onPreviewStarted() {}

        @Override
        public void onZoomChanged(float zoom) {}

        @Override
        public void onPreviewStopped() {}

        @Override
        public void onStartRecording() {}

        @Override
        public void onFinishRecording(String filePath) {}

        @Override
        public void onShotFinished(String filePath) {}

        @Override
        public void onError(Error error) {}
    });
  • Start/stop preview in onResume()/onPause():
    @Override
    protected void onResume() {
        super.onResume();
        photographer.startPreview();
    }

    @Override
    protected void onPause() {
        photographer.stopPreview();
        super.onPause();
    }
  • PhotographerHelper is your friend:
photographerHelper = new PhotographerHelper(photographer); // init with photographer
photographerHelper.setFileDir(Commons.MEDIA_DIR); // set directory for image/video saving
photographerHelper.flip(); // flip back/front camera
photographerHelper.switchMode(); // switch between image capture/video record

See a complete usage in the app sample code.

Credits

License

See the LICENSE file.

Comments
  • After captured Image or recorded video

    After captured Image or recorded video

    After completed camera operations , I want to show image or video in other actvity . Is that true ?

    In PhotographerActivity.java i added to announcingNewFile method these codes. Is it enogh or must i do something about stop camera or backgroundtasks?

    Thank you

                ` private void announcingNewFile(String filePath) {
    	Toast.makeText(PhotographerActivity.this, "File: " + filePath, Toast.LENGTH_SHORT).show();
    	Utils.addMediaToGallery(PhotographerActivity.this, filePath);
    	
    	finish();
    
    	try {
    		Thread.sleep(500);
    	} catch (InterruptedException e) {
    		e.printStackTrace();
    	}
    
    
    	Intent intent = new Intent(PhotographerActivity.this,PreviewActivity.class);
    	intent.putExtra("mode",0);
    	intent.putExtra("file",filePath);
    	startActivity(intent);
    
    }`
    
    opened by mskocabay 4
  • cant integrate into existing project

    cant integrate into existing project

    when implementation 'com.github.duanhong169:camera:1.0.3' is added to app/build.gradle, sync reports this error:

    duplicate value for resource 'attr/mode' with config ''.

    opened by suarezu 3
  • video codec not supported

    video codec not supported

    When I make a video with this library ( com.github.duanhong169:camera:1.0.3 ) and when i click to play this video ,video not played just voice and writes "video codec not supported"

    Can you help me for this problem how to fix ?

    question 
    opened by FidanBacaj 3
  • About ImageSize save problem

    About ImageSize save problem

    I am using huawei for devoloping. when i was image mode imageSize return 4608*3456 from Camere2Photographer.java . and seems toast message your image saved . But not saved in files folder. When i choose imageSize from dialog smaller size , saved.

    I tried in samsung phone no problem. All supported images saved. It is problem about Huawai phones ? How can i solve this problem.

    Thank you.

    opened by mskocabay 2
  • can not switch camera between front and back

    can not switch camera between front and back

    click flip button in PhotographerActivity photographerhelper modify setFacing(int newFacing) method newFacing field actually change but camera is still old

    opened by xuhaohaogithub 0
  • Android Access denied error while using Camera

    Android Access denied error while using Camera

    I'm getting error Access denied finding property "persist.vendor.camera.privapp.list" and access denied finding property "camera.aux.packagelist" for Android api 28 and above ie 8 and 9 android versions.Please help me.

    opened by Baburao4790 1
  • Crash: CameraAccessException: The camera device has encountered a serious error

    Crash: CameraAccessException: The camera device has encountered a serious error

    Android API 21 this is Library not working.

    My logcat;

    
    09-18 10:37:03.569 5606-5636/top.defaults.cameraapp E/GED: Failed to get GED Log Buf, err(0)
    09-18 10:37:26.807 5606-5679/top.defaults.cameraapp E/MALI: get_target_buffer:967: winsysp_window_buffer_get failed 12299
    09-18 10:37:26.846 5606-5679/top.defaults.cameraapp E/MALI: gles_state_set_error_internal:56: GLES ctx: 0x7f91006008, error code:0x505
    09-18 10:37:26.874 5606-5679/top.defaults.cameraapp E/CameraDeviceGLThread-0: Received exception on GL render thread: 
        java.lang.IllegalStateException: glDrawArrays: GLES20 error: 0x505
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkGlError(SurfaceTextureRenderer.java:537)
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawFrame(SurfaceTextureRenderer.java:346)
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:726)
            at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:105)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.os.HandlerThread.run(HandlerThread.java:61)
    09-18 10:37:26.978 5606-5606/top.defaults.cameraapp E/CameraCaptureSession: Session 1: Exception while stopping repeating: 
        android.hardware.camera2.CameraAccessException: The camera device has encountered a serious error
            at android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:1478)
            at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:677)
            at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:328)
            at top.defaults.camera.Camera2Photographer.closePreviewSession(Camera2Photographer.java:700)
            at top.defaults.camera.Camera2Photographer.closeCamera(Camera2Photographer.java:687)
            at top.defaults.camera.Camera2Photographer.stopPreview(Camera2Photographer.java:452)
            at top.defaults.camera.Camera2Photographer$1.onError(Camera2Photographer.java:130)
            at android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks$1.run(CameraDeviceImpl.java:1207)
            at android.os.Handler.handleCallback(Handler.java:815)
            at android.os.Handler.dispatchMessage(Handler.java:104)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5624)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
    09-18 10:37:30.830 5606-5678/top.defaults.cameraapp E/RequestThread-0: Timed out while waiting for request to complete.
    09-18 10:37:30.831 5606-5678/top.defaults.cameraapp E/CameraDeviceState: Cannot receive result while in state: 0
    09-18 10:37:30.831 5606-5678/top.defaults.cameraapp E/CameraDeviceState: Cannot receive result while in state: 0
    09-18 10:37:30.834 5606-5678/top.defaults.cameraapp E/CameraDeviceState: Cannot receive result while in state: 0
    09-18 10:37:30.957 5606-5623/top.defaults.cameraapp E/BufferQueueProducer: [unnamed-5606-2](this:0x7f91234000,id:2,api:4,p:245,c:-1) queueBuffer: BufferQueue has been abandoned
    09-18 10:37:30.958 5606-5684/top.defaults.cameraapp E/BufferQueueProducer: [unnamed-5606-2](this:0x7f91234000,id:2,api:4,p:245,c:-1) dequeueBuffer: BufferQueue has been abandoned
    09-18 10:37:30.988 5606-5624/top.defaults.cameraapp E/BufferQueueProducer: [unnamed-5606-2](this:0x7f91234000,id:2,api:4,p:245,c:-1) queueBuffer: BufferQueue has been abandoned
    09-18 10:37:30.990 5606-5623/top.defaults.cameraapp E/BufferQueueProducer: [unnamed-5606-2](this:0x7f91234000,id:2,api:4,p:245,c:-1) dequeueBuffer: BufferQueue has been abandoned
    09-18 10:37:31.449 5606-5684/top.defaults.cameraapp E/BufferQueueProducer: [unnamed-5606-2](this:0x7f91234000,id:2,api:4,p:245,c:-1) cancelBuffer: BufferQueue has been abandoned
    09-18 10:37:31.450 5606-5678/top.defaults.cameraapp E/BufferQueueProducer: [unnamed-5606-2](this:0x7f91234000,id:2,api:4,p:245,c:-1) query: BufferQueue has been abandoned
    09-18 10:37:31.621 5606-5606/top.defaults.cameraapp E/PhotographerActivity: Error happens: The camera device has encountered a serious error
    09-18 10:37:31.623 5606-5606/top.defaults.cameraapp E/AndroidRuntime: FATAL EXCEPTION: main
        Process: top.defaults.cameraapp, PID: 5606
        java.lang.RuntimeException: MediaRecorder is not initialized
            at top.defaults.camera.Camera2Photographer.throwIfNoMediaRecorder(Camera2Photographer.java:260)
            at top.defaults.camera.Camera2Photographer.startRecording(Camera2Photographer.java:798)
            at top.defaults.cameraapp.PhotographerActivity.action(PhotographerActivity.java:166)
            at top.defaults.cameraapp.PhotographerActivity_ViewBinding$5.doClick(PhotographerActivity_ViewBinding.java:93)
            at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
            at android.view.View.performClick(View.java:4848)
            at android.view.View$PerformClick.run(View.java:20260)
            at android.os.Handler.handleCallback(Handler.java:815)
            at android.os.Handler.dispatchMessage(Handler.java:104)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5624)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
    
    
    opened by adaonder 1
Releases(1.0.3)
Owner
Hong Duan
To be a better man. Focus on Android development.
Hong Duan
A new camera app for GrapheneOS based on the modern CameraX library.

This is the new GrapheneOS Camera app based on Android's modern CameraX library. It's currently in the alpha phase and isn't yet included in GrapheneO

GrapheneOS 513 Jan 1, 2023
A library to take picture easy, transform your data in different format and save photos in your device

A Magic library to take photos and select pictures in Android. In a simple way and if you need it also save the pictures in device, and facial recogni

Fabian Rosales (Frosquivel Developer) 331 Nov 20, 2022
Quick photo and video camera with a flash, customizable resolution and no ads.

Simple Camera A camera with flash, zoom and no ads. The camera is usable for both photo taking and video recording. You can switch between front and r

Simple Mobile Tools 644 Jan 7, 2023
Powerful custom Android Camera with granular control over the video quality and filesize, restricting recordings to landscape only.

LandscapeVideoCamera Highly flexible Android Camera which offers granular control over the video quality and filesize, while restricting recordings to

Jeroen Mols 1.2k Nov 22, 2022
Android camera and serial communication utility that interacts with another device via a USB connection.

PVIT-Payload-Source Android camera and serial communication utility that interacts with another device via a USB connection. PVIT = Palos Verdes Insti

Zeroz & Onez 1 Nov 10, 2021
NguyenThienAn06105 - This repository consist of the code to read camera data on ESP8266 and a simple Android application to visualize the …

MLX90640-HeatCamera This repository consist of the code to read camera data on ESP8266 and a simple Android application to visualize the result. There

Christian Hein 0 Jan 1, 2022
Measures human heart rate using camera and flash light.

Heart-Rate-Ometer Introduction Measures human heart rate using camera and flash light. How-it-works https://github.com/phishman3579/android-heart-rate

Jan Rabe 81 Jun 29, 2022
Wrapper around the android Camera class that simplifies its usage

EasyCamera Wrapper around the android Camera class that simplifies its usage (read more about the process) Usage: // the surface where the preview wil

Bozhidar Bozhanov 642 Nov 27, 2022
Android library to choose image from gallery or camera with option to compress result image

ImagePicker Android library to choose image from gallery or camera with option to compress result image. Download Add this to your project's build.gra

Mayank Nagwanshi 73 May 20, 2022
An Android App that uses Machine Learning to recognize the objects in an image captured from the phone's camera

Camera App ?? Description An Android App that uses Machine Learning (ML) to recognize the objects in an image captured from the phone's camera. • Allo

Shikeya Anderson 1 Dec 31, 2022
Android application to preview, record (MediaRecorder), and fetch each image from both front and rear cameras simultaneously

DuoCamera ?? Overview Android application to preview, record (MediaRecorder) and fetch each image from both front and rear cameras simultaneously. The

Igor Lashkov 5 Nov 28, 2022
Instagram like Image Picker for Android

ImagePicker A simple Instagram like library to select images from the gallery and camera. Screenshot Usage For full example, please refer to the sampl

Akvelon 21 Sep 28, 2022
Image Picker library for Android

Ronnie-Image-Picker Asks for Camera and storage permission and return uri of the images taken or picked from the gallery. Min Api Level: 16 Build Syst

Ronnie Otieno 33 Nov 19, 2022
Bootcamp - Digital Innovation One e NTT Data - Aula de Recursos Nativos do Android

Recursos-Nativos-Android -Foto e Camera Bootcamp - Digital Innovation One e NTT Data - Aula de Recursos Nativos do Android Tutorial de uso da bibliote

Anne Nicolle Zimmermann 0 Jan 3, 2022
Snappy - an android camerax library for taking snapshot fast & simple

Snappy is an android camerax library for taking snapshot fast & simple. Easy to integrate, 100% Kotlin & jetpack compose driven.

Nils Druyen 16 Dec 15, 2022
It's finally easy to take photos/videos via camera or get photos/videos from gallery on Android.

Shutter-Android It's finally easy to take photos/videos via camera or get photos/videos from gallery on Android. What is Shutter? Shutter is an Androi

Levi Bostian 56 Oct 3, 2022
It's finally easy to take photos/videos via camera or get photos/videos from gallery on Android.

Shutter-Android It's finally easy to take photos/videos via camera or get photos/videos from gallery on Android. What is Shutter? Shutter is an Androi

Levi Bostian 56 Oct 3, 2022
Camera is a library for Android built using the Camera2 API.

Camera Camera is a library for Android built using the Camera2 API. The Camera2 API allows users to capture RAW images, i.e. unprocessed pixel data di

binishmatheww 4 Dec 15, 2022
Camera Folder: "Take a Photo from Camera" everywhere where you can open a photo file

When "Camera Folder" is installed many Android-Apps that can open jpg files can also take a photo from camera.

k3b 18 Dec 20, 2022