2

Just as the title say, is it possible to programmatically interact/configure with native Camera app on Android device?

For example, I am trying to have the native camera app to take a picture without having to physically touch the capture button. I'm not wanting to use internal-timer feature in the camera app since this requires physical touch to the button as well.

So simply put, I want to know if it is possible to take a picture without physically touching the capture button.

Thanks in advance for the reply.

JWL
  • 63
  • 3
  • 12

3 Answers3

2

is it possible to programmatically interact/configure with native Camera app on Android device?

Not from an ordinary Android app.

Also, please note that there are several thousand Android device models. There are hundreds of different "native Camera app" implementations across those device models, as device manufacturers often implement their own. Your question implies that you think that there is a single "native Camera app", which is not the case.

For an individual device model, or perhaps a closely related family of devices, with a rooted device, you might be able to work something out with simulated user input. However, the myriad of camera apps means that you would need different rules for each app.

Also, if you are only concerned about your own device, you can use the uiautomator test system to control third-party apps, but that requires a connection to your development machine, as the tests are run from there.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the reply @CommonsWare, I am aware of that so that is why I am trying to interact with the native camera app itself and not making another camera app to work with this case; since it won't be compatible with all of the devices. The case is, I am trying to use this feature on many different devices not just one. So I want to know how I would approach to actually interact with the app itself. So what you're saying is it is impossible to interact with the native camera app itself? – JWL Mar 02 '16 at 23:03
  • 1
    @JWL: **There is no "native camera app"**. There are hundreds of different pre-installed camera apps across thousands of device models. You are welcome to use `ACTION_IMAGE_CAPTURE` to request that one take a picture for you, but there is no convention, let alone requirement, that any of those hundreds of pre-installed camera apps allow third-party apps to actually click the "take-picture" button (or whatever that particular app uses to let the user indicate that the app should take a picture). – CommonsWare Mar 02 '16 at 23:06
  • @JWL: "not making another camera app to work with this case; since it won't be compatible with all of the devices" -- well, done right, it *will* be compatible with most, if perhaps not all, of the devices. There are hundreds of third-party camera apps available on the Play Store, after all. – CommonsWare Mar 02 '16 at 23:08
  • I thank you very much @CommonsWare for thorough response. It took time to fully understand what you have said. So now, let me give you just one quick example and see if you could tell me if this case may be possible: So if I were to open the pre-installed camera app (using intent from 3rd party app or whatever I need to use), it will take a photo by itself without the user physically touching the app after n seconds. I glanced at ACTION_IMAGE_CAPTURE but it appears to me as it is just an intent to call the camera app itself and doesn't really take a photo by itself? – JWL Mar 02 '16 at 23:31
  • if and only if the last example I gave you is possible in any way, I would appreciate it very much if I could know how to approach the solution in any way. Thanks again. – JWL Mar 02 '16 at 23:31
  • @JWL: "it will take a photo by itself without the user physically touching the app after n seconds" -- I covered this in my answer. This is not possible from an ordinary Android app. – CommonsWare Mar 02 '16 at 23:39
1

It is possible. Just forget about "native Camera app" and use Camera/Camera2 API directly.

Some time ago I tried to make a background service taking pictire periodically, detects face and measure eyes distance to prevent my little dougter watching tab too close, but this was fail because tab camera angle was too narrow to take all her face.

I posted part of this app here (this code use depricated Camera interface. It was replaced by Camera2 interface since API21):

public void onCreate() {
    super.onCreate();
    mContext = getApplicationContext();
    surfaceTexture = new SurfaceTexture(0);
}

public void takePictire() {
    Camera cam = openFrontCamera(mContext);
    if (cam != null) {
        try {
            cam.setPreviewTexture(surfaceTexture);
            cam.startPreview();
            cam.takePicture(null, null, mPicture);
        } catch (Exception ex) {
            Log.d(LOG_TAG, "Can't take picture!");
        }
    }
}

private static Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(data), null, bfo);
        // Eye distance detection here and saving data
        camera.stopPreview();
        camera.release();
    }
};

/* Check if this device has a camera */
private static Camera openFrontCamera(Context context) {
    try {
        boolean hasCamera = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
        if (hasCamera) {
            int cameraCount = 0;
            Camera cam = null;
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            cameraCount = Camera.getNumberOfCameras();
            for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
                Camera.getCameraInfo(camIdx, cameraInfo);
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    try {
                        cam = Camera.open(camIdx);
                    } catch (RuntimeException e) {
                        Log.e(LOG_TAG, "Camera failed to open: " + e.getLocalizedMessage());
                    }
                }
            }

            return cam;
        }
    } catch (Exception ex) {
        Log.d(LOG_TAG, "Can't open front camera");
    }

    return null;
}

Some additional info. To use this code you app should have camera permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />
Alexandr Shutko
  • 1,857
  • 2
  • 20
  • 27
  • Hi @Alexandr Shutko I know that it will work with a third party app. However, I specifically asked for the usage of native camera app. Thanks though. – JWL Mar 04 '16 at 00:53
0

Yes, You can capture image without any user input also in background without frame. Take a look here . Hope this help you!

Community
  • 1
  • 1