示例#1
0
  private Memory toPointer(String input) {
    byte[] data = Native.toByteArray(input);
    Memory pointer = new Memory(data.length + 1);
    pointer.write(0, data, 0, data.length);
    pointer.setByte(data.length, (byte) 0);

    return pointer;
  }
示例#2
0
  /**
   * 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;
  }