9

我正在开发一个名为 LMCT(让我复制它)的应用程序,它是用 Java 编程的,我只需要知道如何从 Java 刻录到 DVD/CD。

欢迎任何示例、API 或链接。

4

1 回答 1

9

我已经使用COM4JIMAPI完成了这项工作(仅适用于 Windows)。

此示例代码使用系统中的第一个 CD 刻录设备刻录 ISO 文件:

File isoFile = new File("myimage.iso");

IDiscMaster2 dm = ClassFactory.createMsftDiscMaster2();
int count = dm.count();

//Pick the first recorder on the system
String recorderUniqueId = null;
for (int i = 0; i < count; i++)
{
    String cur = dm.item(i);
    recorderUniqueId = cur;
}

IDiscRecorder2 recorder = ClassFactory.createMsftDiscRecorder2();
recorder.initializeDiscRecorder(recorderUniqueId);
System.out.println("Using recorder: " + recorder.vendorId() + " " + recorder.productId());

IIsoImageManager imageManager = StreamClassFactory.createMsftIsoImageManager();
imageManager.setPath(isoFile.getAbsolutePath());
imageManager.validate();
System.out.println("ISO Validation successful: " + isoFile.getAbsolutePath());

IDiscFormat2DataExt discData = ClassFactoryExt.createMsftDiscFormat2Data();
discData.recorder(recorder);
discData.clientName(IsoMan.class.getSimpleName());
int mediaStatus = discData.currentMediaStatusExt();
System.out.println("Media status: " + mediaStatus);
if ((mediaStatus & IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_WRITE_PROTECTED.comEnumValue()) != 0)
    throw new RuntimeException("Media is write protected / not empty.");

//Check if disc is empty
int addr = discData.nextWritableAddress();
if (addr != 0)
    throw new RuntimeException("Disc is not empty, not writing.");

IStream isoStream = imageManager.stream();

System.out.println("Writing CD");
discData.write(isoStream);
System.out.println("Finished writing");

我不得不稍微补充一下 COM4J 生成的类,因为生成器的工作并不完美:

/**
 * Defines methods to create COM objects
 */
public abstract class ClassFactoryExt {
    private ClassFactoryExt() {} // instanciation is not allowed

    /**
     * Microsoft IMAPIv2 Data Writer
     */
    public static com.ms.imapi2.IDiscFormat2DataExt createMsftDiscFormat2Data() {
        return COM4J.createInstance( com.ms.imapi2.IDiscFormat2DataExt.class, "{2735412A-7F64-5B0F-8F00-5D77AFBE261E}" );
    }
}

/**
 * Data Writer
 */
@IID("{27354153-9F64-5B0F-8F00-5D77AFBE261E}")
public interface IDiscFormat2DataExt extends com.ms.imapi2.IDiscFormat2Data {
    /**
     * The state (usability) of the current media
     */
    @VTID(18)
    //com.ms.imapi2.IMAPI_FORMAT2_DATA_MEDIA_STATE currentMediaStatus();
    int currentMediaStatusExt();
}

/**
 * Defines methods to create COM objects
 */
public abstract class StreamClassFactory {
    private StreamClassFactory() {} // instanciation is not allowed


    /**
     * Boot options
     */
    public static com.ms.imapi2.IBootOptions createBootOptions() {
        return COM4J.createInstance( com.ms.imapi2.IBootOptions.class, "{2C941FCE-975B-59BE-A960-9A2A262853A5}" );
    }

    /**
     * File system image
     */
    public static com.ms.imapi2.IFileSystemImage3 createMsftFileSystemImage() {
        return COM4J.createInstance( com.ms.imapi2.IFileSystemImage3.class, "{2C941FC5-975B-59BE-A960-9A2A262853A5}" );
    }

    /**
     * Microsoft IMAPIv2 Iso Image Manager
     */
    public static com.ms.imapi2.IIsoImageManager createMsftIsoImageManager() {
        return COM4J.createInstance( com.ms.imapi2.IIsoImageManager.class, "{CEEE3B62-8F56-4056-869B-EF16917E3EFC}" );
    }
}
于 2011-12-19T02:53:31.977 回答