Skip to content

Commit

Permalink
copy again gphoto2-java to the project as I suspect my modding causes…
Browse files Browse the repository at this point in the history
… the issue that the app cannot reach the camera

always gets gp_camera_get_config failed with GP_ERROR_IO_USB_CLAIM #-53: Could not claim the USB device
  • Loading branch information
szigyi committed Mar 20, 2021
1 parent 4218ec8 commit efc0bda
Show file tree
Hide file tree
Showing 10 changed files with 1,230 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ libraryDependencies ++= Seq(
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.2",
"com.github.pureconfig" %% "pureconfig" % "0.14.1",
"org.rogach" %% "scallop" % "4.0.2",
"org.gphoto" % "gphoto2-java" % "1.5",
"net.java.dev.jna" % "jna" % "4.2.2",
"org.typelevel" %% "cats-effect" % "2.3.3",
"org.scalatest" %% "scalatest" % "3.2.5" % Test
)
6 changes: 3 additions & 3 deletions src/main/scala/hu/szigyi/ettl/hal/GCamera.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hu.szigyi.ettl.hal

import GFile.saveTo
import org.gphoto2.{CameraFileMod, CameraMod, CameraWidgets}
import org.gphoto2.{CameraFile, Camera, CameraWidgets}

import java.nio.file.Path
import scala.util.Try
Expand All @@ -13,7 +13,7 @@ trait GCamera {
}

class GCameraImpl extends GCamera {
private val c = new CameraMod
private val c = new Camera

override def initialize: Try[Unit] =
Try(c.initialize())
Expand Down Expand Up @@ -63,7 +63,7 @@ object GFile {
}
}

class GFileImpl(f: CameraFileMod) extends GFile {
class GFileImpl(f: CameraFile) extends GFile {
override def close: Try[Unit] =
Try(f.close())
override def saveImageTo(imageBasePath: Path): Try[Path] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@
import org.gphoto2.jna.GPhoto2Native;
import org.gphoto2.jna.GPhoto2Native.CameraFilePath;

import java.io.Closeable;
import java.io.IOException;

/**
* Represents a camera. Thread-unsafe.
* @author Martin Vysny
*/
public class CameraMod extends Camera {
public class Camera implements Closeable {

final Pointer camera;

/**
* Creates a reference to the first connected camera.
*/
public CameraMod() {
public Camera() {
final PointerByReference ref = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_camera_new(ref), "gp_camera_new");
camera = ref.getValue();
Expand Down Expand Up @@ -97,10 +98,10 @@ private void checkNotClosed() {
* Captures a quick preview image on the camera.
* @return camera file, never null. Must be closed afterwards.
*/
public CameraFileMod capturePreview() {
public CameraFile capturePreview() {
checkNotClosed();
boolean returnedOk = false;
final CameraFileMod cfile = new CameraFileMod();
final CameraFile cfile = new CameraFile();
try {
CameraUtils.check(GPhoto2Native.INSTANCE.gp_camera_capture_preview(camera, cfile.cf, CameraList.CONTEXT), "gp_camera_capture_preview");
returnedOk = true;
Expand All @@ -125,11 +126,11 @@ public CameraWidgets newConfiguration() {
* Captures a full-quality image image on the camera.
* @return camera file, never null. Must be closed afterwards.
*/
public CameraFileMod captureImage() {
public CameraFile captureImage() {
checkNotClosed();
final CameraFilePath path = new CameraFilePath.ByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_camera_capture(camera, GPhoto2Native.GP_CAPTURE_IMAGE, path, CameraList.CONTEXT), "gp_camera_capture");
final PathMod p = new PathMod(path);
final Path p = new Path(path);
return p.newFile(camera);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
import com.sun.jna.ptr.PointerByReference;
import org.gphoto2.jna.GPhoto2Native;

public class CameraFileMod extends CameraFile {
import java.io.Closeable;

public class CameraFile implements Closeable {

final Pointer cf;

public PathMod p;
public Path p;

/**
* Creates a new file link. The file is not yet linked to any particular camera file - the link is performed later on, by invoking gphoto functions.
*/
CameraFileMod() {
CameraFile() {
final PointerByReference p = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_file_new(p), "gp_file_new");
cf = p.getValue();
}

CameraFileMod(PathMod path) {
CameraFile(Path path) {
this();
this.p = path;
}
Expand Down Expand Up @@ -52,7 +54,7 @@ void unref() {
CameraUtils.check(GPhoto2Native.INSTANCE.gp_file_unref(cf), "gp_file_unref");
}

public PathMod getPath() {
public Path getPath() {
return this.p;
}

Expand Down
145 changes: 145 additions & 0 deletions src/main/scala/org/gphoto2/CameraList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* Java bindings for the libgphoto2 library.
* Copyright (C) 2011 Innovatrics s.r.o.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.gphoto2;

import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import java.io.Closeable;
import java.util.regex.Pattern;
import org.gphoto2.jna.GPhoto2Native;

/**
* Lists connected cameras.
* @author Martin Vysny
*/
public class CameraList implements Closeable {

public static final Pointer CONTEXT;

static {
CONTEXT = GPhoto2Native.INSTANCE.gp_context_new();
if (CONTEXT == null) {
throw new RuntimeException("Failed to get context");
}
}
private final Pointer list;

/**
* Enumerates connected cameras.
*/
public CameraList() {
list = newList();
populateList();
}

private static Pointer newList() {
final PointerByReference ref = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_new(ref), "gp_list_new");
return ref.getValue();
}
private static final Pattern USB_MATCH = Pattern.compile("usb:\\d+,\\d+");

private void populateList() {
final Pointer tempList = newList();
try {
final PointerByReference ref = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_abilities_list_new(ref), "gp_abilities_list_new");
final Pointer cameraAbilitiesList = ref.getValue();
try {
final PointerByReference ref2 = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_port_info_list_new(ref2), "gp_port_info_list_new");
final Pointer portInfoList = ref2.getValue();
try {
CameraUtils.check(GPhoto2Native.INSTANCE.gp_port_info_list_load(portInfoList), "gp_port_info_list_load");
CameraUtils.check(GPhoto2Native.INSTANCE.gp_abilities_list_load(cameraAbilitiesList, CONTEXT), "gp_abilities_list_load");
CameraUtils.check(GPhoto2Native.INSTANCE.gp_abilities_list_detect(cameraAbilitiesList, portInfoList, tempList, CONTEXT), "gp_abilities_list_detect");
final int count = CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_count(tempList), "gp_list_count");
for (int i = 0; i < count; i++) {
final PointerByReference pmodel = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_get_name(tempList, i, pmodel), "gp_list_get_name");
final String model = pmodel.getValue().getString(0);
final PointerByReference pvalue = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_get_value(tempList, i, pvalue), "gp_list_get_value");
final String path = pvalue.getValue().getString(0);
if (USB_MATCH.matcher(path).matches()) {
CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_append(list, model, path), "gp_list_append");
}
}
} finally {
GPhoto2Native.INSTANCE.gp_port_info_list_free(portInfoList);
}
} finally {
GPhoto2Native.INSTANCE.gp_abilities_list_free(cameraAbilitiesList);
}
} finally {
GPhoto2Native.INSTANCE.gp_list_free(tempList);
}
}

/**
* Returns a displayable name of the camera.
* @param i the camera index, must be 0 .. {@link #getCount()} - 1.
* @return the displayable camera name, never null, for example Canon EOS 1000D
*/
public String getModel(int i) {
final PointerByReference pmodel = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_get_name(list, i, pmodel), "gp_list_get_name");
return pmodel.getValue().getString(0);
}

/**
* Returns a displayable name of the port to which the camera is connected.
* @param i the camera index, must be 0 .. {@link #getCount()} - 1.
* @return the displayable camera name, never null, for example usb:002,019
*/
public String getPort(int i) {
final PointerByReference pvalue = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_get_value(list, i, pvalue), "gp_list_get_value");
return pvalue.getValue().getString(0);
}

/**
* Returns connected camera count.
* @return connected camera count.
*/
public int getCount() {
return CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_count(list), "gp_list_count");
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("CameraList[");
for (int i = 0; i < getCount(); i++) {
sb.append(getModel(i)).append(':').append(getPort(i)).append(", ");
}
sb.append("]");
return sb.toString();
}

public void close() {
CameraUtils.check(GPhoto2Native.INSTANCE.gp_list_free(list), "gp_list_free");
}

public Pointer getPortInfo(int index) {
final PointerByReference result = new PointerByReference();
CameraUtils.check(GPhoto2Native.INSTANCE.gp_port_info_list_get_info(list, index, result), "gp_port_info_list_get_info");
return result.getValue();
}
}
Loading

0 comments on commit efc0bda

Please sign in to comment.