java获取系统dpi_Java Printer.getDPI方法代码示例

import org.eclipse.swt.printing.Printer; //导入方法依赖的package包/类

/** {@inheritDoc} */

@Override

public void run()

{

// Get snapshot. Disposed at end of printing

final Image snapshot = media_pool.get(ImageConverter.convertToSWT(graph.getImage()));

if (snapshot == null)

{

Logger.getLogger(getClass().getName()).fine("Cannot obtain image");

return;

}

// Printer GUI

final PrintDialog dlg = new PrintDialog(shell);

PrinterData data = dlg.open();

if (data == null)

{

Logger.getLogger(getClass().getName()).fine("Cannot obtain printer");

snapshot.dispose();

return;

}

// At least on Linux, access to SWT Printer must be on UI thread.

// Printing in other thread can deadlock with UI thread.

final Printer printer = new Printer(data);

try

{

if (!printer.startJob("Data Browser"))

{

Logger.getLogger(getClass().getName()).fine("Cannot start print job");

return;

}

try

{ // Printer page info

final Rectangle area = printer.getClientArea();

final Rectangle trim = printer.computeTrim(0, 0, 0, 0);

final Point dpi = printer.getDPI();

// Compute layout

final Rectangle image_rect = snapshot.getBounds();

// Leave one inch on each border.

// (copied the computeTrim stuff from an SWT example.

// Really no clue...)

final int left_right = dpi.x + trim.x;

final int top_bottom = dpi.y + trim.y;

final int printed_width = area.width - 2*left_right;

// Try to scale height according to on-screen aspect ratio.

final int max_height = area.height - 2*top_bottom;

final int printed_height = Math.min(max_height,

image_rect.height * printed_width / image_rect.width);

// Print one page

printer.startPage();

final GC gc = new GC(printer);

gc.drawImage(snapshot, 0, 0, image_rect.width, image_rect.height,

left_right, top_bottom, printed_width, printed_height);

printer.endPage();

}

finally

{

printer.endJob();

}

}

finally

{

printer.dispose();

snapshot.dispose();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值