【问题标题】:Java: List Cameras that are Plugged InJava:列出已插入的相机
【发布时间】:2011-06-27 19:54:10
【问题描述】:

我的程序当前使用File.listRoots() 获取插入计算机的驱动器列表。但是,当我将相机或 MP3 播放器直接插入计算机(而不是插入存储卡)时,它没有列出,在 Windows 资源管理器中也没有驱动器号。例如,这是我的相机的位置:

Computer\Canon PowerShot SD750\Removable storage

如何同时列出没有驱动器号的相机/其他设备?我认为这将需要某种 JNI 库,但显然我不确定。

谢谢!

附:无奈之下,我确实尝试列出Computer\的内容;当然没用。


更新:我在这里发现了这个问题:Portable Device Path on Windows;这正是我遇到的问题,但没有解决方案。

【问题讨论】:

    标签: java camera device


    【解决方案1】:

    Java 7 在这方面有一些很有前途的类,比如这个: http://download.java.net/jdk7/docs/api/java/nio/file/FileSystem.html

    假设您也需要它在 Java 6 上工作,我建议您运行一个 shell 脚本并解析它的输出。 在 Windows 上,你可以运行 mountvol,在 Unix/MacOS X mount 等。当然,解析输出会有些乏味,你不得不担心你的应用程序运行的每个操作系统,但是嘿,至少......不确定是什么。 ...它有效吗?

    Here 是一些在 Windows 上看起来很有帮助的示例代码:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Volume")
    
    For Each objItem In colItems
        WScript.Echo "Automount: " & objItem.Automount
        WScript.Echo "Block Size: " & objItem.BlockSize
        WScript.Echo "Capacity: " & objItem.Capacity
        WScript.Echo "Caption: " & objItem.Caption
        WScript.Echo "Compressed: " & objItem.Compressed
        WScript.Echo "Device ID: " & objItem.DeviceID
        WScript.Echo "Dirty Bit Set: " & objItem.DirtyBitSet
        WScript.Echo "Drive Letter: " & objItem.DriveLetter
        WScript.Echo "Drive Type: " & objItem.DriveType
        WScript.Echo "File System: " & objItem.FileSystem
        WScript.Echo "Free Space: " & objItem.FreeSpace
        WScript.Echo "Indexing Enabled: " & objItem.IndexingEnabled
        WScript.Echo "Label: " & objItem.Label
        WScript.Echo "Maximum File Name Length: " & objItem.MaximumFileNameLength
        WScript.Echo "Name: " & objItem.Name
        WScript.Echo "Quotas Enabled: " & objItem.QuotasEnabled
        WScript.Echo "Quotas Incomplete: " & objItem.QuotasIncomplete
        WScript.Echo "Quotas Rebuilding: " & objItem.QuotasRebuilding
        WScript.Echo "Serial Number: " & objItem.SerialNumber
        WScript.Echo "Supports Disk Quotas: " & objItem.SupportsDiskQuotas
        WScript.Echo "Supports File-Based Compression: " & _
            objItem.SupportsFileBasedCompression
        WScript.Echo
    Next
    

    这是我为我的电子书阅读器得到的输出:

    Automount: True
    Block Size: 4096
    Capacity: 999120896
    Caption: G:\
    Compressed: 
    Device ID: \\?\Volume{8e3b4ce5-a124-11e0-9d2b-e30c5839642d}\
    Dirty Bit Set: False
    Drive Letter: G:
    Drive Type: 2
    File System: FAT32
    Free Space: 663683072
    Indexing Enabled: 
    Label: PocketBook9
    Maximum File Name Length: 255
    Name: G:\
    Quotas Enabled: 
    Quotas Incomplete: 
    Quotas Rebuilding: 
    Serial Number: 1276177233
    Supports Disk Quotas: False
    Supports File-Based Compression: False
    

    【讨论】:

    • 太糟糕了,我在 JRE6 上。您能否提供一个包含我问题中信息的示例 shell 命令?我现在正在为 Windows 开发这个程序。
    • 我认为没有办法获取该库并在 Java 6 中使用它?
    • @Jonah 我添加了一些示例脚本,您可以从中获取信息。我认为您不能使用 java6 中的 /Java7 功能。
    • @MK:我在控制台中运行了那个 VBScript,但它确实没有列出设备。
    • @MK:您的电子书阅读器地址是如何出现在 Windows 资源管理器中的?
    【解决方案2】:

    使用JMTP库解决上述问题

    http://code.google.com/p/jmtp/

    这是我的代码

    打包 jmtp;
    import be.derycke.pieter.com.COMException;
    import be.derycke.pieter.com.Guid;
    import java.io.*;
    import java.math.BigInteger;
    import jmtp.PortableDevice;
    import jmtp.*;
    
    public class Jmtp {
    
        public static void main(String[] args) {
            PortableDeviceManager manager = new PortableDeviceManager();
            PortableDevice device = manager.getDevices()[0];
            // Connect to my mp3-player
            device.open();
    
            System.out.println(device.getModel());
    
            System.out.println("---------------");
    
            // Iterate over deviceObjects
            for (PortableDeviceObject object : device.getRootObjects()) {
                // If the object is a storage object
                if (object instanceof PortableDeviceStorageObject) {
                    PortableDeviceStorageObject storage = (PortableDeviceStorageObject) object;
    
                    for (PortableDeviceObject o2 : storage.getChildObjects()) {
    //                    
    //                        BigInteger bigInteger1 = new BigInteger("123456789");
    //                        File file = new File("c:/JavaAppletSigningGuide.pdf");
    //                        try {
    //                            storage.addAudioObject(file, "jj", "jj", bigInteger1);
    //                        } catch (Exception e) {
    //                            //System.out.println("Exception e = " + e);
    //                        }
    //                    
    
                        System.out.println(o2.getOriginalFileName());
                    }
                }
            }
    
            manager.getDevices()[0].close();
    
        }
    }
    

    不要忘记将 jmtp.dll 文件(随 jmtp 下载提供)添加为本机库以获取更多信息,请参阅我在 上的回答
    http://stackoverflow.com/questions/12798530/includes-native-library-in-netbeans

    【讨论】:

      【解决方案3】:

      这可能不是您要寻找的答案,但是将它们分配给驱动器号不是一种选择吗?您通常可以使用 Windows 上的 USB 设备手动执行此操作,方法是使用我的电脑 > 右键单击​​ > 管理 > 存储。

      JMF(Java 媒体框架)中的 CaptureDeviceManager 可能可能可以帮助您,但我有点怀疑。

      【讨论】:

      • 这将是一个商业发行的软件,所以很遗憾这不是一个选项。
      【解决方案4】:

      也许你可以看看 Morena Framework http://www.gnome.sk/Twain/jtp.htmlv(似乎是开源的,但有点贵;虽然有免费评估版),它适用于兼容 TWAIN 的扫描仪/相机(Windows/MAC)或SANE 兼容(Linux 或其他 unix 风格),要获取已连接设备的列表,您可以这样做:

      import SK.gnome.morena.*;
      import SK.gnome.twain.*;
      
      public class Test
      {
          public static void main(String[] args) throws Exception
          {
              TwainSource[] sources=TwainManager.listSources();
              if(sources == null) return;
              for(int i = 0; i < sources.length; i++)
              {
                  System.out.println("Twain source is: " + ts.toString());
              }
          }
      }
      

      也许这会有所帮助,如果没有,我认为 JMF 可能是一个可能的解决方案。

      【讨论】:

      • 我认为这不是我要找的东西,那是图像设备,我只是想访问一个驱动器。
      猜你喜欢
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 2016-07-16
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多