Search code examples
androidcameracompatibility

Taking a photo using the front camera on Android 2.1 and 2.2


I can take a photo using the front camera in android Gingerbread 2.3 and above with this code:

package com.system.settings;
import java.io.File;
import java.io.FileOutputStream;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;

public class FrontPhoto implements PictureCallback 
{
    private static Camera  camera;
    private final  Context context;

    public FrontPhoto(Context context) 
    {
        this.context = context;
    }

    public void onPictureTaken(byte[] data, android.hardware.Camera camera) 
    {
        File pictureFile = new File("/sdcard/FrontPhoto.jpg");
        try 
        {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        }
        catch (Throwable t){}
    }

    public static void Take(Context ctx)
    {
        if (FrontCameraPresent(ctx) == true)
        {           
            camera.takePicture(null, null, new FrontPhoto(ctx));
        }
    }

    private static boolean FrontCameraPresent(Context ctx) 
    {
        if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
        {
            boolean result       = false;
            int     numOfCameras = Camera.getNumberOfCameras();
            for (int i = 0; i < numOfCameras; i++) 
            {
                CameraInfo info = new CameraInfo();
                Camera.getCameraInfo(i, info);
                if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 
                {
                    camera = Camera.open(i);
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
}

But I want to be able to use this code for 2.2 and 2.1 versions. To do this, I put the Camera class from 2.3 source in my project, changed it and ran. But I get an error

Can't find getNumberOfCameras native method.

I read all the answers on this topic on this site. I also tried this code, but it also gives an error and this is not a universal way, because it is only suitable for some devices:

Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480);
mCamera.setParameters(parameters);

My question is: how to make compatibility with older versions. how to include all of the methods used by the Camera class of 2.3 in my project and all the native methods it needs.


Solution

  • To do this, I put the Camera class from 2.3 source in my project, changed it and ran.

    That will not work with any framework class, let alone Camera.

    But I get an error Can't find getNumberOfCameras native method.

    That is because Android will ignore your android.hardware.Camera class and use its own. You do not control the runtime classpath, and Android's system classes will always take precedence over any with the same name and package that happen to be in your APK.

    You could refactor your copy into some other package, but then you'll crash on all of the native methods.

    My question is: how to make compatibility with older versions

    You can't. There was no front-facing camera support in Android prior to Android 2.3.

    Certain device manufacturers may have set up front-facing cameras on their devices, and some subset of those manufacturers published instructions for how developers can use them, like the camera-id code that you showed above. You are welcome to research all of those instructions, detect those specific devices using the Build class, then apply those instructions where available.

    how to include all of the methods used by the Camera class of 2.3 in my project and all the native methods it needs.

    You can't. The Android OS prior to Android 2.3 is not going to magically sprout front-facing camera support, no matter what you put in your project.