GPhoto2 Java拍照

时间:2018-03-16 07:52:28

标签: java jna libgphoto2

我使用此库github:gphoto2-jna访问我的尼康。我预览工作(实时预览并将预览图像保存到磁盘)但我陷入了拍摄普通照片并将其保存到磁盘的困难。 我认为我的问题只是一个指针问题,但由于我不使用这些东西,我很难弄清楚这个问题:D

这是我制作的代码:

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;
    }
}

如果您对如何解决此问题有任何疑问,请与我们联系。提前谢谢!

0 个答案:

没有答案