Java:列出已插入的相机

发布于 2024-11-17 15:57:22 字数 530 浏览 1 评论 0原文

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

Computer\Canon PowerShot SD750\Removable storage

我怎样才能列出没有驱动器号的相机/其他设备?我认为这需要某种 JNI 库,但我显然不确定。

谢谢!

PS 出于无奈,我确实尝试列出Computer\的内容;当然没用。


更新:我在这里发现了这个问题:Windows 上的便携式设备路径;这正是我遇到的问题,但那里没有提供解决方案。

My program currently gets a list of drives plugged into the computer with File.listRoots(). But, when I plug a camera or an MP3 player into the computer directly (instead of inserting the memory card), it's not listed, nor does it have a drive letter in Windows Explorer. For example, here's the location of my camera:

Computer\Canon PowerShot SD750\Removable storage

How can I also list cameras/other devices that do not have a drive letter? I assume this will require a JNI library of some sort, but I don't know for sure obviously.

Thanks!

P.S. Out of desperation, I did try to list the contents of Computer\; it didn't work of course.


Update: I found this question here: Portable Device Path on Windows ; that's exactly the problem I'm having, but there is no solution laid out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╄→承喏 2024-11-24 15:57:22

Java 7 在这一领域有一些看起来很有前途的类,例如:
http://download.java.net/jdk7/docs /api/java/nio/file/FileSystem.html

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

这里是一些在 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

Java 7 has some promising looking classes in this area, like this one:
http://download.java.net/jdk7/docs/api/java/nio/file/FileSystem.html

Assuming that you need it to work on Java 6 as well, I would suggest running a shell script and parsing its output.
On Windows you could run mountvol, on Unix/MacOS X mount etc. Of course parsing the output would be somewhat tedious and you would have to worry about every OS your app runs on, but hey, at least... not sure what.... it works?

Here is some sample code which seems helpful on 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

Here is the output I got for my ebook reader:

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
那支青花 2024-11-24 15:57:22

使用 JMTP 库解决上述问题

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

是我的代码

    package 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();

    }
}

Donot forget add jmtp.dll files (that comes up with jmtp download) as a native library for more info see my answer on

http://stackoverflow.com/questions/12798530/including-native-library-in-netbeans

The solution to above problem using JMTP library on

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

Here is my code

    package 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();

    }
}

Donot forget add jmtp.dll files (that comes up with jmtp download) as a native library for more info see my answer on

http://stackoverflow.com/questions/12798530/including-native-library-in-netbeans
许仙没带伞 2024-11-24 15:57:22

这可能不是您正在寻找的答案,但是将它们分配给驱动器号不是一个选项吗?通常,您可以使用 Windows 上的“我的电脑”>“USB 设备”手动执行此操作。右键单击>管理>贮存。

JMF(java 媒体框架)中的 CaptureDeviceManager可能可以帮助您,但我对此表示怀疑。

This may not be the answer you're looking for, but is assigning them to a drive letter not an option? You can usually manually do this with USB devices on Windows using My Computer > right-click > Manage > Storage.

It's possible that CaptureDeviceManager in JMF (java media framework) could help you but I kind of doubt it.

潇烟暮雨 2024-11-24 15:57:22

也许你可以看看 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 是一个可能的解决方案。

Maybe you can take a look at Morena Framework http://www.gnome.sk/Twain/jtp.htmlv (seems to be open source, but a little expensive; though there is a free evaluation version), it is for TWAIN compatible scanners/cameras (Windows/MAC) or SANE compatible (Linux or other unix flavor), to get a list of connected devices, you can do this:

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());
        }
    }
}

Maybe that could help,if not I think maybe JMF is a possible solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文