/** * Creates a stream and corresponding live view image. Don't forget to call {@link * CanonUtils#release(edsdk.bindings.EdSdkLibrary.EdsBaseRef.ByReference...) release()} on the * returned array when you are done using it or you will cause a memory leak! * * @param camera the camera to query * @return EdsEvfImageRef.ByReference and EdsStreamRef.ByReference as indexes 0 and 1 respectively */ public static EdsBaseRef.ByReference[] getLiveViewImageReference(final EdsCameraRef camera) { EdsError err = EdsError.EDS_ERR_OK; final EdsStreamRef.ByReference streamRef = new EdsStreamRef.ByReference(); final EdsEvfImageRef.ByReference imageRef = new EdsEvfImageRef.ByReference(); // Create memory stream. err = CanonUtils.toEdsError( CanonCamera.EDSDK.EdsCreateMemoryStream(new NativeLong(0), streamRef)); if (err != EdsError.EDS_ERR_OK) { System.err.println( "Failed to download live view image, memory stream could not be created (error " + err.value() + ": " + err.name() + " - " + err.description() + ")"); CanonUtils.release(imageRef, streamRef); return null; } err = CanonUtils.toEdsError( CanonCamera.EDSDK.EdsCreateEvfImageRef( new EdsStreamRef(streamRef.getPointer().getPointer(0)), imageRef)); if (err != EdsError.EDS_ERR_OK) { System.err.println( "Failed to download live view image, image ref could not be created (error " + err.value() + ": " + err.name() + " - " + err.description() + ")"); CanonUtils.release(imageRef, streamRef); return null; } // Now try to follow the guidelines from // http://tech.groups.yahoo.com/group/CanonSDK/message/1225 // instead of what the edsdk example has to offer! // Download live view image data. err = CanonUtils.toEdsError(CanonCamera.EDSDK.EdsDownloadEvfImage(camera, imageRef.getValue())); if (err != EdsError.EDS_ERR_OK) { /* * System.err.println( "Failed to download live view image (error " * + * err.value() + ": " + err.name() + " - " + * err.description() + ")" ); */ CanonUtils.release(imageRef, streamRef); return null; } return new EdsBaseRef.ByReference[] {imageRef, streamRef}; }
/** * Convert a bunch of eds objects * * @param value * @return */ public static void release(final EdsBaseRef... objects) { for (final EdsBaseRef obj : objects) { if (obj != null) { CanonCamera.EDSDK.EdsRelease(obj); } } }
public static EdsError setCapacity(final EdsCameraRef ref, final int capacity) { final EdsCapacity.ByValue edsCapacity = new EdsCapacity.ByValue(); edsCapacity.bytesPerSector = new NativeLong(512); edsCapacity.numberOfFreeClusters = new NativeLong(capacity / edsCapacity.bytesPerSector.intValue()); edsCapacity.reset = 1; return CanonUtils.toEdsError(CanonCamera.EDSDK.EdsSetCapacity(ref, edsCapacity)); }
/** * Query property data * * @param ref Reference to the camera/image/liveview object * @param property Property id * @param param * @param size Data size in bytes * @return Current long value of the property */ public static EdsError getPropertyData( final EdsBaseRef ref, final EdsPropertyID property, final long param, final long size, final Pointer data) { return CanonUtils.toEdsError( CanonCamera.EDSDK.EdsGetPropertyData( ref, new NativeLong(property.value()), new NativeLong(param), new NativeLong(size), data)); }
/** * Finds the filename for a directory item * * @param directoryItem The item you want to download * @return Either null, or the filename of the item */ public static EdsDirectoryItemInfo getDirectoryItemInfo(final EdsDirectoryItemRef directoryItem) { EdsError err = EdsError.EDS_ERR_OK; final EdsDirectoryItemInfo dirItemInfo = new EdsDirectoryItemInfo(); try { err = CanonUtils.toEdsError( CanonCamera.EDSDK.EdsGetDirectoryItemInfo(directoryItem, dirItemInfo)); } catch (final Exception e) { e.printStackTrace(); } return err == EdsError.EDS_ERR_OK ? dirItemInfo : null; }
/** * Get the size of a property * * @param ref Reference to the camera/image/liveview object * @param property the property id * @param param * @return Size in bytes */ public static long getPropertySize( final EdsBaseRef ref, final EdsPropertyID property, final long param) { final int bufferSize = 1; final IntBuffer type = IntBuffer.allocate(bufferSize); final NativeLongByReference number = new NativeLongByReference(new NativeLong(bufferSize)); final EdsError err = CanonUtils.toEdsError( CanonCamera.EDSDK.EdsGetPropertySize( ref, new NativeLong(property.value()), new NativeLong(param), type, number)); if (err == EdsError.EDS_ERR_OK) { // System.out.println( "> property size = " + number.getValue().longValue() ); return number.getValue().longValue(); } return -1; }
/** * @param ref The camera to get the available property settings of * @param property One of the supported EdsPropertyID values * @return The EdsPropertyDesc containing the available settings for the given property */ public static EdsPropertyDesc getPropertyDesc(final EdsBaseRef ref, final EdsPropertyID property) throws IllegalArgumentException { final EdsPropertyDesc propertyDesc = new EdsPropertyDesc(); final EdsError err = CanonUtils.toEdsError( CanonCamera.EDSDK.EdsGetPropertyDesc( ref, new NativeLong(property.value()), propertyDesc)); if (err == EdsError.EDS_ERR_OK) { // System.out.println( "> available values = " + propertyDesc.numElements ); return propertyDesc; } throw new IllegalArgumentException( "An error occurred while getting detailed " + property.name() + " data (error " + err.value() + ": " + err.name() + " - " + err.description() + ")"); }
/** * Returns the data type of a specified property * * @param ref Reference to the camera * @param property Property id * @param param Name of the parameter * @return The data type, or null if the parameter isn't supported, or unknown if something else * goes wrong. */ public static EdsDataType getPropertyType( final EdsBaseRef ref, final EdsPropertyID property, final long param) { final int bufferSize = 1; final IntBuffer type = IntBuffer.allocate(bufferSize); final NativeLongByReference number = new NativeLongByReference(new NativeLong(bufferSize)); final EdsError err = CanonUtils.toEdsError( CanonCamera.EDSDK.EdsGetPropertySize( ref, new NativeLong(property.value()), new NativeLong(param), type, number)); if (err == EdsError.EDS_ERR_OK) { final EdsDataType edsDataType = EdsDataType.enumOfValue(type.get(0)); if (edsDataType != null) { // System.out.println( " > property type = " + edsDataType.value() + " : " + // edsDataType.name() + " : " + edsDataType.description() ); return edsDataType; } } else if (err == EdsError.EDS_ERR_NOT_SUPPORTED) { return null; } return EdsDataType.kEdsDataType_Unknown; }
/** * 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; }
/** * 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; }