I used this lib github:gphoto2-jna to access my Nikon. I got the preview working (live preview and save the preview image to disk) but I got stuck in capturing a normal photo and save it to the disk. I think my problem is just a pointer thing, but since I'm not working with this stuff its hard for me to figure out this problem :D
This is the code I made:
public void takePhoto() {
CameraFilePath captureImage = captureImage(_camera, _context);
PointerByReference pbRef = new PointerByReference(captureImage.getPointer());
byte[] cameraFileData = getCameraFileData(pbRef, _camera, _context); //from demo
BufferedImage image = ImageIO.read(new ByteArrayInputStream(cameraFileData));
//write image to disk
}
public static PointerByReference captureImage(Camera camera, PointerByReference context) {
CameraFilePath.ByReference cameraPath = new CameraFilePath.ByReference();
PointerByReference pbRef = new PointerByReference(cameraPath.getPointer());
{
check(Gphoto2Library.INSTANCE.gp_file_new(pbRef));
pbRef.setPointer(pbRef.getValue());
}
synchronized (LOCK_OBJECT_CAPTURE) {
check(Gphoto2Library.INSTANCE.gp_camera_capture(camera, 0, cameraPath, context));
}
return cameraPath;
}
public static byte[] getCameraFileData(PointerByReference cameraFile, Camera camera, PointerByReference context) {
PointerByReference pref = new PointerByReference();
LongByReference longByRef = new LongByReference();
int captureRes = check(Gphoto2Library.INSTANCE.gp_file_get_data_and_size(cameraFile, pref, longByRef));
if (captureRes >= 0) {
// throws NullPointer
return pref.getValue().getByteArray(0, (int) longByRef.getValue());
} else {
return null;
}
}
Please let me know if you have any idea how to fix this problem. Thanks in advance!