便携式的方式来确定打印机是物理的还是虚拟的 [英] Portable way to determining of printer is physical or virtual

查看:166
本文介绍了便携式的方式来确定打印机是物理的还是虚拟的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要直接打印机功能为我的网站,能够区分物理打印机与虚拟打印机(文件)。
$ b $ p

Coupons.com有此功能通过必须由用户安装的本机二进制文件。我宁愿避免这种情况。



SmartSource.com通过Java applet完成它:



有人知道这是怎么完成的吗?我挖掘了一些Java API,并没有看到任何可以让你确定物理与虚拟的东西,除了看名字(这似乎容易出现错误识别)。能够在Java中完成这将是很好的,因为我已经知道如何编写Java小程序。如果不这样做,有没有办法在Flash或Silverlight中做到这一点?



在此先感谢。

<编辑:当之无愧的赏金颁给Jason Sperske谁制定了一个优雅的解决方案。感谢那些分享想法的人,以及那些真正调查过SmartSource.com解决方案的人(如Adrian)。

好的,这里是我迄今为止发现的(这不是一个彻底的测试,但它是一个有趣的问题尝试和解决)。看起来可能有一些帮助,看看PrinterJob类中的validatePage()方法是如何工作的。看起来,如果打印机作业是虚拟的,而不是任何尝试设置页面的ImageableArea,将始终返回一个完全等于默认页面ImageableArea的值,而试图对相同的实际打印机返回的值则会略小一些纸张的边缘是由打印机机构保存的,这个问题有什么帮助,如果你在调用验证之前就问打印机的默认特性,你会得到一个乐观的结果,如果你把它和一个有效的响应进行比较,你可以做一个简单如果测试。我已经写了一些代码,这似乎与我的桌面上的图像打印机和真正的打印机(再次这并不是详尽的,但它可以作为一个起点)工作

  import java.awt.print。*; 

import javax.print.PrintService;
import javax。 print.attribute.Attribute;

public class DetectFilePrinter {

public static void main(String [] args){
Printe rJob job = PrinterJob.getPrinterJob();
PrintService printer = job.getPrintService();
System.out.println(Printer Name:+ printer.getName());

System.out.println(printer.toString());
PageFormat page = job.defaultPage();
double default_width = page.getWidth();
double default_height = page.getHeight();

Paper paper = new Paper();
paper.setImageableArea(0,0,Double.MAX_VALUE,Double.MAX_VALUE);
page.setPaper(paper);

PageFormat fixed_pa​​ge = job.validatePage(page);

double fixed_width = fixed_pa​​ge.getImageableWidth();
double fixed_height = fixed_pa​​ge.getImageableHeight();

//到目前为止,在调用validatePage()
if(default_height == fixed_height&& amp; amp; amp;& ;default_width == fixed_width){
System.out.println(这看起来像一个\图像打印机\);
} else {
System.out.println(这看起来像一个\真正的打印机\);
}
}
}


I need direct-to-printer functionality for my website, with the ability to distinguish a physical printer from a virtual printer (file).

Coupons.com has this functionality via a native binary which must be installed by the user. I'd prefer to avoid that.

SmartSource.com does it via Java applet:

Does anybody know how this is done? I dug through that Java APIs a bit, and don't see anything that would let you determine physical vs virtual, except looking at the name (that seems prone to misidentification). It would be nice to be able to do it in Java, because I already know how to write Java applets. Failing that, is there a way to do this in Flash or Silverlight?

Thanks in advance.

EDIT: Well deserved bounty awarded to Jason Sperske who worked out an elegant solution. Thanks to those of you who shared ideas, as well as those who actually investigated SmartSource.com's solution (like Adrian).

解决方案

OK here is what I've found so far (this is not an exhaustive test by any means, but it has been a fun problem to try and tackle). it seems that there might be some help by looking at how the validatePage() method in the PrinterJob class works. It seems that if a printer job is virtual than any attempt to set a page's ImageableArea will always return a value exactly equal to the default pages ImageableArea while an attempt to to the same to a real printer will return slightly smaller values (to account for the edges of the paper being held by the printer mechanics. What helps for this problem is that if you just ask a printer for it's default characteristics before calling validate you get an optimistic result, and if you compare this to a validated response you can do a simple if test. I've written some code for this that seems to work with the image printers and real printers I have on my desktop (again this isn't exhaustive, but it could work as a starting point)

import java.awt.print.*;

import javax.print.PrintService;
import javax.print.attribute.Attribute;

public class DetectFilePrinter {

  public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintService printer = job.getPrintService();
    System.out.println("Printer Name:"+printer.getName());

    System.out.println(printer.toString());
    PageFormat page = job.defaultPage();
    double default_width = page.getWidth();
    double default_height = page.getHeight();

    Paper paper = new Paper();
    paper.setImageableArea(0, 0, Double.MAX_VALUE, Double.MAX_VALUE);
    page.setPaper(paper);

    PageFormat fixed_page = job.validatePage(page);

    double fixed_width = fixed_page.getImageableWidth();
    double fixed_height = fixed_page.getImageableHeight();

    //So far all of my tested "image printers" return the same 
    //height and width after calling validatePage()
    if(default_height == fixed_height && default_width == fixed_width) {
      System.out.println("This looks like a \"image printer\"");
    } else {
      System.out.println("This looks like a \"real printer\"");
    }
  }
}

这篇关于便携式的方式来确定打印机是物理的还是虚拟的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆