Package org.gphoto2

Source Code of org.gphoto2.Camera

package org.gphoto2;

import static org.gphoto2.Context.cContext;
import static org.gphoto2.GPhotoException.check;

import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;

import org.gphoto2.CameraWidget.ContainerWidget;
import org.gphoto2.CameraWidget.RadioWidget;
import org.gphoto2.jna.CameraAbilitiesStruct;
import org.gphoto2.jna.CameraFilePath;
import org.gphoto2.jna.Gphoto2Library;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;


public class Camera implements Closeable {
  public enum CaptureType {
    IMAGE, VIDEO, SOUND;
  }
  public static class Event {
    public enum Type {
      UNKNOWN, TIMEOUT, FILE_ADDED, FOLDER_ADDED, CAPTURE_COMPLETE
    }
    private final Type type;
    private final String path;
    protected Event(Type type, String path) {
      this.type = type;
      this.path = path;
    }
    public Type getType() {
      return type;
    }
    public String getPath() {
      return path;
    }
    @Override
    public String toString() {
      if (path != null) {
        return type + ": " + path;
      } else {
        return type.toString();
      }
    }
  }
 
  protected Pointer nativePointer;
 
  public Camera(Context context) {
    this(null, null, context);
  }
 
  public Camera(String port, String model, Context context) {
    PointerByReference ptrCamera = new PointerByReference();
    check(Gphoto2Library.INSTANCE.gp_camera_new(ptrCamera));
    nativePointer = ptrCamera.getValue();
   
    if (port != null) {
      PortInfoList portInfoList = new PortInfoList();
      try {
        portInfoList.load();
        check(Gphoto2Library.INSTANCE.gp_camera_set_port_info(
            nativePointer, portInfoList.lookupPath(port).getNativePointer()));
      } finally {
        portInfoList.close();
      }
    }
   
    if (model != null) {
      CameraAbilitiesList abilityList = new CameraAbilitiesList();
      try {
        abilityList.load(context);
        CameraAbilities abilities = abilityList.lookupModel(model);
        CameraAbilitiesStruct.ByValue abilitiesByValue = (CameraAbilitiesStruct.ByValue)Structure.newInstance(
            CameraAbilitiesStruct.ByValue.class, abilities.getNativeStruct().getPointer());
        abilitiesByValue.read();
        check(Gphoto2Library.INSTANCE.gp_camera_set_abilities(nativePointer, abilitiesByValue));
      } finally {
        abilityList.close();
      }
    }
   
    check(Gphoto2Library.INSTANCE.gp_camera_init(nativePointer, Context.cContext(context)));
  }
   
 
  @Override
  public void close() {
    close(null);
  }
 
  public void close(Context context) {
    if (nativePointer != null) {
      check(Gphoto2Library.INSTANCE.gp_camera_exit(nativePointer, cContext(context)));
      check(Gphoto2Library.INSTANCE.gp_camera_free(nativePointer));
      nativePointer = null;
    }
  }
 
  @Override
  protected void finalize() throws Throwable {
    close();
    super.finalize();
  }
 
  protected Pointer getNativePointer() {
    if (nativePointer == null) {
      throw new IllegalStateException("closed");
    } else {
      return nativePointer;
    }
  }

  public int getPortSpeed() {
    return check(Gphoto2Library.INSTANCE.gp_camera_get_port_speed(getNativePointer()));
  }
 
  public void setPortSpeed(int speed) {
    check(Gphoto2Library.INSTANCE.gp_camera_set_port_speed(getNativePointer(), speed));
  }
 
  public String getSummary(Context context) {
    Memory buf = new Memory(32*1024);
    check(Gphoto2Library.INSTANCE.gp_camera_get_summary(getNativePointer(), buf, cContext(context)));
    return buf.getString(0);
  }
  public String getManual(Context context) {
    Memory buf = new Memory(32*1024);
    check(Gphoto2Library.INSTANCE.gp_camera_get_manual(getNativePointer(), buf, cContext(context)));
    return buf.getString(0);
  }
  public String getAbout(Context context) {
    Memory buf = new Memory(32*1024);
    check(Gphoto2Library.INSTANCE.gp_camera_get_about(getNativePointer(), buf, cContext(context)));
    return buf.getString(0);
  }
 
  public CameraWidget getConfig(Context context) {
    PointerByReference ptrWidget = new PointerByReference();
    check(Gphoto2Library.INSTANCE.gp_camera_get_config(getNativePointer(), ptrWidget, cContext(context)));
    return CameraWidget.getByNativePointer(ptrWidget.getValue());
  }
 
  public void setConfig(CameraWidget config, Context context) {
    check(Gphoto2Library.INSTANCE.gp_camera_set_config(getNativePointer(), config.getNativePointer(), cContext(context)));
  }
 
  public String capture(CaptureType captureType, Context context) {
    CameraFilePath.ByReference path = new CameraFilePath.ByReference();
    check(Gphoto2Library.INSTANCE.gp_camera_capture(getNativePointer(), captureType.ordinal(), path, cContext(context)));
    return Native.toString(path.folder) + Native.toString(path.name);
  }

  public String capturePreview(Context context) {
    CameraFilePath.ByReference path = new CameraFilePath.ByReference();
    check(Gphoto2Library.INSTANCE.gp_camera_capture_preview(getNativePointer(), path, cContext(context)));
    return Native.toString(path.folder) + Native.toString(path.name);
  }
 
  public void triggerCapture(Context context) {
    check(Gphoto2Library.INSTANCE.gp_camera_trigger_capture(getNativePointer(), cContext(context)));
  }
 
  public Event waitForEvent(int timeout, Context context) {
    IntByReference eventType = new IntByReference();
    PointerByReference eventData = new PointerByReference();
    check(Gphoto2Library.INSTANCE.gp_camera_wait_for_event(getNativePointer(), timeout, eventType, eventData, cContext(context)));
    Event.Type type = Event.Type.values()[eventType.getValue()];
    String path = null;
    if (type == Event.Type.FILE_ADDED || type == Event.Type.FILE_ADDED) {
      path = eventData.getValue().getString(0);
    }
    return new Event(type, path);
  }
   
  //TODO
  //public CameraStorageInformation getStorageInformation() {
 
  @SuppressWarnings("resource")
  public static void main(String[] args) throws IOException, InterruptedException {
    GPhoto2.installSharedLibraries();
    System.out.println(Arrays.toString(GPhoto2.getLibraryVersion(false)));
   
    Context ctx = new Context(new ContextListener.LoggerAdapter());
    System.setErr(System.out);
   
    CameraList cameraList = new CameraList();
    cameraList.detect(null, null, ctx);
    System.out.println(cameraList);
   
    Camera camera = new Camera("ptpip:192.168.1.1", "PTP/IP Camera", ctx);
    System.out.println("=== About:\n" + camera.getAbout(null));
    //System.out.println("=== Manual:\n" + camera.getManual(null));
    System.out.println("=== Summary:\n" + camera.getSummary(null));
    System.out.println(camera.getPortSpeed());
    //camera.triggerCapture(ctx);
    //camera.triggerCapture(ctx);
   
    ContainerWidget widget = (ContainerWidget)camera.getConfig(ctx);
    System.out.println(widget);
    System.out.println();
   
    RadioWidget ev = (RadioWidget)widget.getChildByLabel("Exposure Compensation");
    for (String s : ev.getChoices()) {
      ev.setValue(s);
      Thread.sleep(5000);
      camera.setConfig(widget, null);
      camera.triggerCapture(null);
    }

   
    /*
    System.out.println("=== ABOUT ===");
    System.out.println(camera.getAbout(null));
    System.out.println("=== Summary ===");
    System.out.println(camera.getSummary(null));
    System.out.println("=== Manual ===");
    System.out.println(camera.getManual(null));*/
  }
TOP

Related Classes of org.gphoto2.Camera

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.