- Home /
Android Unity ARCore camera access
I have a Unity3D Augmented Reality app that is using ARCore. I have an Android plugin from which I'm trying to take a photo with the camera in the background (no UI or button press required). I've tried both the front and back camera but in both cases, I am met with an exception saying java io ioexception setpreviewtexture failed. I have also noticed that I get a warning on my AR app saying something like "another app is using the camera. Tap this app to try and XYZ", so it's definitely something to do with the camera being blocked by ARCore.
I know my code works as doing this in a non-AR app works and a picture is taken in the background.
My code is nothing fancy and in fact, is a direct copy from this SO post.
private void takePhoto() {
System.out.println( "Preparing to take photo");
Camera camera = null;
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
SystemClock.sleep(1000);
Camera.getCameraInfo(camIdx, cameraInfo);
try {
camera = Camera.open(camIdx);
} catch (RuntimeException e) {
System.out.println("Camera not available: " + camIdx);
camera = null;
//e.printStackTrace();
}
try{
if (null == camera) {
System.out.println("Could not get camera instance");
}else{
System.out.println("Got the camera, creating the dummy surface texture");
//SurfaceTexture dummySurfaceTextureF = new SurfaceTexture(0);
try {
//camera.setPreviewTexture(dummySurfaceTextureF);
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
} catch (Exception e) {
System.out.println("Could not set the surface preview texture");
e.printStackTrace();
}
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "PictureFront_" + "_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File mainPicture = new File(filename);
addImageFile(mainPicture);
try {
FileOutputStream fos = new FileOutputStream(mainPicture);
fos.write(data);
fos.close();
System.out.println("image saved");
} catch (Exception error) {
System.out.println("Image could not be saved");
}
camera.release();
}
});
}
}catch (Exception e){
camera.release();
}
}
$$anonymous$$aybe your camera resource is taken by Unity and you get this error.
AR Foundation has an API to get a camera image on CPU: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@1.0/manual/cpu-camera-image.html
But I need to do this through my Android plugin, as I mentioned. The code I posted is Java (not c#) from my Android plugin. The camera is most certainly blocked/consumed by ARCore.