以下代碼片段應回傳列印機可用的介質托盤。
但是,對于某些驅動程式,特別是Ricoh PCL6 Driver for Universal Print和HP Universal Printing PCL 6,除了列印機托盤之外,這些驅動程式還列出了紙張型別,例如Recycled、Thick、Matte等。
據我所知,OpenJDK在DC_BINNAMES呼叫DeviceCapabilities. OpenJDK似乎根本沒有在源代碼中使用DC_MEDIATYPENAMES,所以我不希望 egPurple Paper成為一個可查詢的屬性,但它在從 Ricoh 驅動程式查詢托盤時列出。
那么有什么問題呢?這些 PCL 6 驅動程式只是被竊聽了嗎?有DeviceCapabilities錯嗎?或者該錯誤是否存在于 OpenJDK 中?
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.standard.Media;
public class TrayListing {
public static void main(String ... args) {
String printerName = "HP LaserJet ..."; // TODO: change this to the actual printer name
PrintService[] allPrinters = PrintServiceLookup.lookupPrintServices(null, null);
for(PrintService ps : allPrinters) {
if(ps.getName().equalsIgnoreCase(printerName)) {
// loop over media trays
System.out.println("\n\nFound MediaTray:");
// Some HP, Ricoh printers/drivers list items that aren't printer trays, such as paper types
for(Media m : (Media[])ps.getSupportedAttributeValues(Media.class, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null)) {
if (m instanceof javax.print.attribute.standard.MediaTray) {
System.out.println("- " m " (" m.getClass().getName() ")");
}
}
}
}
}
}
附加關鍵字:
PCL XL Feature Reference
uj5u.com熱心網友回復:
驅動程式被竊聽。存在變通方法,但它們很復雜。
短:
- 匹配驅動程式名稱
Ricoh|HP和PCL6|PCL 6 - 過濾任何大于 1000 的托盤 ID
長:
某些驅動程式(例如 HP)會在其他區域正確地暴露列印機托盤,例如:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\<PRINTER_NAME>\PrinterDriverDataInputSlotInputSlotDisplayNames
...但是,對于理光等驅動程式而言,情況并非如此。
在檢查了很多驅動程式(HP、Ricoh、Xerox、Konica 等)后,我將問題歸結為以下幾點:
- PCL6 驅動程式
- 惠普或理光作為供應商
在兩家供應商的情況下,該DC_BINS值始終 > 1000,這在 PCL6MediaSource規范中有部分解釋,參考:
“...通過 將值替換為列舉值來選擇外部輸入托盤
1通過。例如, = 、=等...”24882558first external input tray9second external input tray
雖然沒有什么具體的 1,000,而且 Xerox 等供應商使用超過 7000 的值而沒有錯誤。也就是說,對于有問題的供應商,觀察到的是,當值 > 1,000 時,它們往往是實際有效值MediaType(不是MediaSource值),但增加了 1,000。
奇怪的是,這僅限于 HP 和 Ricoh,不適用于其他 PCL 驅動程式。例如:
- 柯尼卡使用的托盤ID
1000=LCT或“大容量托盤”,這是有效的。 - Xerox 提供 PCL6 驅動程式,但通常使用高于 1000 的托盤 ID,例如
7153=Tray 1、7154=Tray 2。 - 眾所周知,Ricoh
1025在其驅動程式的 PCL5 版本中使用了托盤 ID,這是一個有效的托盤值1025=Auto Tray Select,但對于他們的 PCL6 驅動程式來說似乎不是這種情況,它MediaType混合了這些值。
所以為了“修復”這個問題,我撰寫了一系列自定義決議來找出驅動程式供應商和托盤 ID。
要從 Java 中找到托盤 ID:
// Get default printer
PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
for(Media mediaTray : (Media[])ps.getSupportedAttributeValues(Media.class, null, null)) {
// Warning: Reflective operation on Windows-only class
Method method = ps.getClass().getMethod("findTrayID", MediaTray.class);
Object trayId = method.invoke(ps, new Object[]{mediaTray});
System.out.println(trayId);
}
從 Java 獲取驅動程式供應商:
- This part is much more complex and requires a third-party dependency, JNA. For this reason I'm omitting the code, but instead I will provide the steps:
- Reading the registry is as simple as calling
Advapi32Util.registryGetStringValue(...) - Knowing which registry String value to obtain the driver name is a complex operation, but the following areas are helpful:
HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers\<PRINTER_NAME>\Printer DriverHKLM\Software\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\<PRINTER_NAME>\Printers\<GUID>
Calculating the driver name is some nuanced code, but can be used reliably. For a comprehensive code example, see here. Take special note of the special character replacement in the keys/values.
Once the driver is found, matching the terms Ricoh, HP, PCL6 and PCL 6 using a regular expression will allow filtering trayIDs over 1,000.
By combining the above techniques of trayId numbering, vendor matching and the keywords "PCL6" and "PCL 6", the bad trays can be filtered out programatically.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/449965.html
