コード例 #1
0
ファイル: CanonUtils.java プロジェクト: Treydone/edsdk4j
  /**
   * Downloads an image and saves it somewhere.
   *
   * @param directoryItem The item you want to download
   * @param destination A path in the filesystem where you want to save the file. Can also be null
   *     or a directory. In case of null the temp directory will be used, in case of a directory the
   *     file name of the item will be used.
   * @param appendFileExtension Adds the extension of the photo onto File to ensure that supplied
   *     File name extension matches the image being downloaded from the camera. This is especially
   *     important if the camera is set to RAW+JPEG where the order of the images is not consistent.
   * @return Either null, or the location the file was ultimately saved to on success.
   */
  public static File download(
      final EdsDirectoryItemRef directoryItem,
      File destination,
      final boolean appendFileExtension) {
    EdsError err = EdsError.EDS_ERR_OK;
    final EdsStreamRef.ByReference stream = new EdsStreamRef.ByReference();
    final EdsDirectoryItemInfo dirItemInfo = new EdsDirectoryItemInfo();

    boolean success = false;

    // final long timeStart = System.currentTimeMillis();

    try {
      err =
          CanonUtils.toEdsError(
              CanonCamera.EDSDK.EdsGetDirectoryItemInfo(directoryItem, dirItemInfo));
      if (err == EdsError.EDS_ERR_OK) {
        if (destination == null) {
          destination = new File(System.getProperty("java.io.tmpdir"));
        }
        if (destination.isDirectory()) {
          destination = new File(destination, Native.toString(dirItemInfo.szFileName));
        } else if (appendFileExtension) {
          final String sourceFileName = Native.toString(dirItemInfo.szFileName);
          final int i = sourceFileName.lastIndexOf(".");
          if (i > 0) {
            final String extension = sourceFileName.substring(i);
            if (!destination.getName().toLowerCase().endsWith(extension)) {
              destination = new File(destination.getPath() + extension);
            }
          }
        }

        if (destination.getParentFile() != null) {
          destination.getParentFile().mkdirs();
        }

        /*
         * System.out.println( "Downloading image " +
         * Native.toString( dirItemInfo.szFileName ) +
         * " to " + destination.getCanonicalPath() );
         */

        // TODO: see if using an EdsCreateMemoryStream would be faster and whether the image could
        // be read directly without saving to file first - see:
        // http://stackoverflow.com/questions/1083446/canon-edsdk-memorystream-image
        err =
            CanonUtils.toEdsError(
                CanonCamera.EDSDK.EdsCreateFileStream(
                    Native.toByteArray(destination.getCanonicalPath()),
                    EdsFileCreateDisposition.kEdsFileCreateDisposition_CreateAlways.value(),
                    EdsAccess.kEdsAccess_ReadWrite.value(),
                    stream));
      }

      if (err == EdsError.EDS_ERR_OK) {
        err =
            CanonUtils.toEdsError(
                CanonCamera.EDSDK.EdsDownload(directoryItem, dirItemInfo.size, stream.getValue()));
      }

      if (err == EdsError.EDS_ERR_OK) {
        /*
         * System.out.println( "Image downloaded in " +
         * ( System.currentTimeMillis() - timeStart ) +
         * " ms" );
         */

        err = CanonUtils.toEdsError(CanonCamera.EDSDK.EdsDownloadComplete(directoryItem));

        success = true;
      }

      if (stream != null) {
        CanonCamera.EDSDK.EdsRelease(stream.getValue());
      }
    } catch (final Exception e) {
      e.printStackTrace();
    }

    return success ? destination : null;
  }
コード例 #2
0
ファイル: CanonUtils.java プロジェクト: Treydone/edsdk4j
  /**
   * Download a live view image from the camera and convert it directly into a bufferd image.
   *
   * @param camera
   * @return
   */
  public static BufferedImage getLiveViewImage(final EdsCameraRef camera) {
    EdsError err = EdsError.EDS_ERR_OK;

    final EdsBaseRef.ByReference[] references = CanonUtils.getLiveViewImageReference(camera);
    if (references != null) {
      final EdsStreamRef.ByReference streamRef = (EdsStreamRef.ByReference) references[1];
      final EdsEvfImageRef.ByReference imageRef = (EdsEvfImageRef.ByReference) references[0];

      //		// Get the incidental data of the image.
      //		NativeLongByReference zoom = new NativeLongByReference( new NativeLong( 0 ) );
      //		Pointer data = zoom.getPointer();
      //		err = getPropertyData( image.getValue(), EdSdkLibrary.kEdsPropID_Evf_ZoomPosition, 0,
      // NativeLong.SIZE, data );
      //		if( err != EdsError.EDS_ERR_OK ){
      //			System.err.println( "Failed to download live view image, zoom value wasn't read (error "+
      // err.value() + ": "+ err.name() + " - " + err.description() + ")" );
      //			return null;
      //		}
      //
      //		// Get the focus and zoom border position
      //		EdsPoint point = new EdsPoint();
      //		data = point.getPointer();
      //		err = getPropertyData( image.getValue(), EdSdkLibrary.kEdsPropID_Evf_ZoomPosition, 0 ,
      // sizeof( point ), data );
      //		if( err != EdsError.EDS_ERR_OK ){
      //			System.err.println( "Failed to download live view image, focus point wasn't read (error
      // "+ err.value() + ": "+ err.name() + " - " + err.description() + ")" );
      //			return null;
      //		}

      final NativeLongByReference length = new NativeLongByReference();
      err = CanonUtils.toEdsError(CanonCamera.EDSDK.EdsGetLength(streamRef.getValue(), length));
      if (err != EdsError.EDS_ERR_OK) {
        System.err.println(
            "Failed to download live view image, failed to read stream length (error "
                + err.value()
                + ": "
                + err.name()
                + " - "
                + err.description()
                + ")");
        CanonUtils.release(imageRef, streamRef);
        return null;
      }

      final PointerByReference ref = new PointerByReference();
      err = CanonUtils.toEdsError(CanonCamera.EDSDK.EdsGetPointer(streamRef.getValue(), ref));
      if (err != EdsError.EDS_ERR_OK) {
        System.err.println(
            "Failed to download live view image, failed to get reference to image in memory (error "
                + err.value()
                + ": "
                + err.name()
                + " - "
                + err.description()
                + ")");
        CanonUtils.release(imageRef, streamRef);
        return null;
      }

      final byte[] data = ref.getValue().getByteArray(0, length.getValue().intValue());
      try {
        final BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
        return img;
      } catch (final IOException e) {
        e.printStackTrace();
      } finally {
        CanonUtils.release(imageRef, streamRef);
      }
    }

    return null;
  }