Matthew Steinhardt
Matthew Steinhardt

Reputation: 332

Accessing the camera using android.hardware.camera2

I want to create an app that allows me to control the flash on the camera, however my research into how to do this has led to a bunch of examples that use the recently depreciated android.hardware.camera api. Can someone point me in the right direction on how I would use androird.hardware.camera2 to make a simple flashlight app?

Upvotes: 1

Views: 7368

Answers (3)

josua josh
josua josh

Reputation: 88

You should edit your question if you just want to make a simple flashlight app. Using the camera with the new android.hardware.camera2 is more complex.

I have tried to make camera preview app using android studio and nexus lg 5x but haven't succeed.

Instead if you just want to turn on the flash light here's the codes:

void torch(){
    /* turn on the flash light */
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        String[] cameraID = new String[]{};
        cameraID = cameraManager.getCameraIdList();
        for(int i = 0; i<cameraID.length; i++){
            Log.e(TAG,cameraID[i]);
        }

        /* camera id is 0 and 1. 0 is the back camera, 1 is the front camera */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cameraManager.setTorchMode(cameraID[0],true); 
            //true means turned on, false, means turned off.
        }

    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera2.full" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MaterialTheme">

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>

MainActivity.java:

package com.example.test;

import android.app.Activity;
import android.graphics.Camera;
import android.hardware.camera2.params.InputConfiguration;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends Activity {

private final static String TAG = "Camera2testJ";
private Size mPreviewSize;

private TextureView mTextureView;
private CameraDevice mCameraDevice;
private CaptureRequest.Builder mPreviewBuilder;
private CameraCaptureSession mPreviewSession;

private Button mBtnShot;

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    mTextureView = (TextureView)findViewById(R.id.texture);
    mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);

    mBtnShot = (Button)findViewById(R.id.btn_takepicture);
    mBtnShot.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.e(TAG, "mBtnShot clicked");
            torch();
        }

    });

}

void torch(){
    /* try the flash light */
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);;
    try {
        String[] cameraID = new String[]{};
        cameraID = cameraManager.getCameraIdList();
        for(int i = 0; i<cameraID.length; i++){
            Log.e(TAG,cameraID[i]);
        }

        /* camera id is 0 and 1. which one is the front or the back camera? */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cameraManager.setTorchMode(cameraID[0],true);
        }

    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

}

Layout activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextureView
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/btn_takepicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/picture"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Upvotes: 2

android.hardware.camera is deprecated.but you can still make use of it and it is working in lollipop and pre-lollipop

I tried it before few days http://www.androidsources.com/index.php/2015/09/19/android-flashlight-app-tutorial/

Upvotes: 0

Rinaldi Segecin
Rinaldi Segecin

Reputation: 517

You should study https://github.com/googlesamples/android-Camera2Basic sample. There shows a way to control the flash with

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                                    CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);

I've been studing a way to get all the frames shown in the preview texture to do some image processing, if you find a way please let me know =D.

Upvotes: 1

Related Questions