救命啊,java 打印 我用java的javax.print和swt的打印机类打印分辨率为4200 * 2850的图片都会出现内存益处和颜色不对呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 实现这个接口Printable就可以了 下边是其中一点点代码. // 印刷設定データを取得する PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); if (pageSettingData.getPaperDirection().equals("LANDSCAPE")) { aset.add(OrientationRequested.LANDSCAPE);// paper.setImageableArea(0,0,pageHeight,pageWidth); } else if (pageSettingData.getPaperDirection().equals("PORTRAIT")) { aset.add(OrientationRequested.PORTRAIT);// paper.setImageableArea(0,0,pageWidth,pageHeight); } aset.add(new Copies(1)); aset.add(new JobName("エリアの印刷", null));还需要计算纸张大小, if (pageSettingData.getPaperDirection().equals("LANDSCAPE")) { if (paperSize.equals("A3")) { pageWidth = 420 / 25.4 * 72; pageHeight = 297 / 25.4 * 72; } else if (paperSize.equals("A4")) { pageWidth = 297 / 25.4 * 72; pageHeight = 210 / 25.4 * 72; } else if (paperSize.equals("A5")) { pageWidth = 210 / 25.4 * 72; pageHeight = 148 / 25.4 * 72; } else if (paperSize.equals("B2")) { pageWidth = 707 / 25.4 * 72; pageHeight = 500 / 25.4 * 72; } else if (paperSize.equals("B3")) { pageWidth = 500 / 25.4 * 72; pageHeight = 353 / 25.4 * 72; } else if (paperSize.equals("B4")) { pageWidth = 353 / 25.4 * 72; pageHeight = 250 / 25.4 * 72; } } else if (pageSettingData.getPaperDirection().equals("PORTRAIT")) { if (paperSize.equals("A3")) { pageHeight = 420 / 25.4 * 72; pageWidth = 297 / 25.4 * 72; } else if (paperSize.equals("A4")) { pageHeight = 297 / 25.4 * 72; pageWidth = 210 / 25.4 * 72; } else if (paperSize.equals("A5")) { pageHeight = 210 / 25.4 * 72; pageWidth = 148 / 25.4 * 72; } else if (paperSize.equals("B2")) { pageHeight = 707 / 25.4 * 72; pageWidth = 500 / 25.4 * 72; } else if (paperSize.equals("B3")) { pageHeight = 500 / 25.4 * 72; pageWidth = 353 / 25.4 * 72; } else if (paperSize.equals("B4")) { pageHeight = 353 / 25.4 * 72; pageWidth = 250 / 25.4 * 72; } }代码很多,只能告诉你这些. public abstract class PhotoPrintService extends Thread { private ImageData tempImageH = null; private ImageData tempImageL = null; private List<Photo> photos = null; private String printerNameH = ""; private String printerNameL = ""; private PaletteData palette = null; private Point DPI_H = null; private Point DPI_L = null; public PhotoPrintService(PaletteData palette, List<Photo> photos, String printerNameH, String printerNameL) { this.palette = palette; this.photos = new LinkedList<Photo>(); this.photos.addAll(photos); this.printerNameH = printerNameH; this.printerNameL = printerNameL; } protected abstract void finish(); @Override public void run() { try { //this.printUseImage(); this.printUseHDC(); } catch (Exception e) { e.printStackTrace(); } finally { finish(); } } private void printUsePrintService() { this.initDPI(); for (Photo photo : photos) { if (photo.getImage() == null || photo.getCount() == 0) { continue; } if (photo.getType() == 0) { photo.getImage().fixImage(6 * DPI_H.x, 4 * DPI_H.y); this.printImage(photo.getImage().save(), printerNameH, photo.getCount()); } else { this.printImage(photo.getImage().save(), printerNameL, photo.getCount()); } } } private void printUseImage() { this.initDPI(); this.createTempImage(60, 40); Printer printerH = findPrinter(printerNameH); Printer printerL = findPrinter(printerNameL); GC gcH = new GC(printerH); GC gcL = new GC(printerL); gcH.setClipping(0, 0, DPI_H.x * 6, DPI_H.y * 4); gcL.setClipping(0, 0, DPI_H.x * 6, DPI_H.y * 4); boolean flagH = false; boolean flagL = false; for (Photo photo : photos) { if (photo.getImage() == null || photo.getCount() == 0) { continue; } if (photo.getType() == 0) { if (flagH == false) { printerH.startJob("photo"); flagH = true; photo.getImage().getImage(tempImageH); this.printImage(tempImageH, printerH, gcH, photo.getCount()); } } else { if (flagL == false) { printerL.startJob("photo"); flagL = true; photo.getImage().getImage(tempImageL); this.printImage(tempImageL, printerL, gcL, photo.getCount()); } } } if (flagH) { printerH.endJob(); } if (flagL) { printerL.endJob(); } gcH.dispose(); gcL.dispose(); printerH.dispose(); printerL.dispose(); } private void printUseHDC() { this.initDPI(); Printer printerH = findPrinter(printerNameH); Printer printerL = findPrinter(printerNameL); GC gcH = new GC(printerH); GC gcL = new GC(printerL); gcH.setClipping(0, 0, DPI_H.x * 3 + 1, DPI_H.y * 2 + 1); gcL.setClipping(0, 0, DPI_H.x * 6 + 1, DPI_H.y * 4 + 1); boolean flagH = false; boolean flagL = false; for (Photo photo : photos) { if (photo.getImage() == null || photo.getCount() == 0) { continue; } if (photo.getType() == 0) { if (flagH == false) { printerH.startJob("photo"); flagH = true; int width = 3 * DPI_H.x; int height = 2 * DPI_H.y; this.printImage(photo.getImage(), printerH, gcH, width, height, photo.getCount()); } } else { if (flagL == false) { printerL.startJob("photo"); flagL = true; int width = 6 * DPI_L.x; int height = 4 * DPI_L.y; this.printImage(photo.getImage(), printerL, gcL, width, height, photo.getCount()); } } } if (flagH) { printerH.endJob(); } if (flagL) { printerL.endJob(); } gcH.dispose(); gcL.dispose(); printerH.dispose(); printerL.dispose(); } private void createTempImage(int width, int height) { try { tempImageH = new ImageData(width * DPI_H.x / 10, height * DPI_H.y / 10, 32, palette); tempImageL = new ImageData(width * DPI_L.x / 10, height * DPI_L.y / 10, 32, palette); System.out.println("h:" + tempImageH.width + "," + tempImageH.height); System.out.println("l:" + tempImageL.width + "," + tempImageL.height); } catch (Exception e) { e.printStackTrace(); } finally { } } private void initDPI() { try { Printer printerH = findPrinter(printerNameH); Printer printerL = findPrinter(printerNameL); DPI_H = printerH.getDPI(); DPI_L = printerL.getDPI(); printerH.dispose(); printerL.dispose(); } catch (Exception e) { e.printStackTrace(); } finally { } } private Printer findPrinter(String name) { PrinterData[] datas = Printer.getPrinterList(); for (PrinterData data : datas) { if (data.name.equalsIgnoreCase(name)) { return new Printer(data); } } return null; } private void printImage(ImageData data, Printer printer, GC gc, int count) { Image image = new Image(printer, data); for (int i = 0; i < count; i++) { printer.startPage(); gc.drawImage(image, 0, 0); printer.endPage(); } image.dispose(); } private void printImage(ImageData data, Printer printer, GC gc, int x, int y) { Image image = new Image(printer, data); gc.drawImage(image, x, y); image.dispose(); } private void printImage(ThumbImage image, Printer printer, GC gc, int width, int height, int count) { System.out.println("size:" + width + "," + height); // for(int i = 0;i<count;i++){ // printer.startPage(); image.print(gc.handle, width, height, count); // printer.endPage(); // } } private void printImage(String filename, String printerName, int count) { try { PrintService service = findPrintService(printerName); DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG; FileInputStream stream = new FileInputStream(filename); Doc doc = new SimpleDoc(stream, flavor, null); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); for (int i = 0; i < count; i++) { service.createPrintJob().print(doc, null); } } catch (Exception e) { e.printStackTrace(); } } private PrintService findPrintService(String printerName) { DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG; AttributeSet attributes = new HashAttributeSet(); PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, attributes); for (PrintService service : services) { if (service.getName().equalsIgnoreCase(printerName)) { return service; } } return null; }} 代码见上面,不管是使用什么方式打印,都出现耗用2G内存的情况,而且是Spoolsv.exe耗用的。private void printUsePrintService()是使用打印APIprivate void printUseHDC()是使用Win API的DC进行打印private void printUseImage() 是使用SWT进行绘制是不是打印机的问题啊,我就只有一个打印机,打印机的DPI为720 java如何获取Flash的播放时长或者是播放的总帧数 请问如何使string类型转int数组 (不是string[]转int[],是string转int[]) 求助,关于大容量数组分页显示的问题 十万火急!!!!!java用字节流读取文件在线等 如何设置JButton的提示信息的背景颜色? HTML標簽問題,急! 关于时间格式转换的问题 怎样把两个文本文件接起来?? 谁有bruceeckel的类库包? 新手:如何获得一个文件夹中所以的文本文件并读写到一个txt文件中 ArrayList<E> 后面的 E 是什么意思? jdk1.6下载及jdk1.6的新特征
下边是其中一点点代码.
// 印刷設定データを取得する
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
if (pageSettingData.getPaperDirection().equals("LANDSCAPE")) {
aset.add(OrientationRequested.LANDSCAPE);
// paper.setImageableArea(0,0,pageHeight,pageWidth);
} else if (pageSettingData.getPaperDirection().equals("PORTRAIT")) {
aset.add(OrientationRequested.PORTRAIT);
// paper.setImageableArea(0,0,pageWidth,pageHeight);
} aset.add(new Copies(1));
aset.add(new JobName("エリアの印刷", null));还需要计算纸张大小,
if (pageSettingData.getPaperDirection().equals("LANDSCAPE")) {
if (paperSize.equals("A3")) {
pageWidth = 420 / 25.4 * 72;
pageHeight = 297 / 25.4 * 72;
} else if (paperSize.equals("A4")) {
pageWidth = 297 / 25.4 * 72;
pageHeight = 210 / 25.4 * 72;
} else if (paperSize.equals("A5")) {
pageWidth = 210 / 25.4 * 72;
pageHeight = 148 / 25.4 * 72;
} else if (paperSize.equals("B2")) {
pageWidth = 707 / 25.4 * 72;
pageHeight = 500 / 25.4 * 72;
} else if (paperSize.equals("B3")) {
pageWidth = 500 / 25.4 * 72;
pageHeight = 353 / 25.4 * 72;
} else if (paperSize.equals("B4")) {
pageWidth = 353 / 25.4 * 72;
pageHeight = 250 / 25.4 * 72;
}
} else if (pageSettingData.getPaperDirection().equals("PORTRAIT")) {
if (paperSize.equals("A3")) {
pageHeight = 420 / 25.4 * 72;
pageWidth = 297 / 25.4 * 72;
} else if (paperSize.equals("A4")) {
pageHeight = 297 / 25.4 * 72;
pageWidth = 210 / 25.4 * 72;
} else if (paperSize.equals("A5")) {
pageHeight = 210 / 25.4 * 72;
pageWidth = 148 / 25.4 * 72;
} else if (paperSize.equals("B2")) {
pageHeight = 707 / 25.4 * 72;
pageWidth = 500 / 25.4 * 72;
} else if (paperSize.equals("B3")) {
pageHeight = 500 / 25.4 * 72;
pageWidth = 353 / 25.4 * 72;
} else if (paperSize.equals("B4")) {
pageHeight = 353 / 25.4 * 72;
pageWidth = 250 / 25.4 * 72;
}
}
代码很多,只能告诉你这些.
this.palette = palette;
this.photos = new LinkedList<Photo>();
this.photos.addAll(photos);
this.printerNameH = printerNameH;
this.printerNameL = printerNameL;
} protected abstract void finish(); @Override
public void run() {
try {
//this.printUseImage();
this.printUseHDC();
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
} private void printUsePrintService() {
this.initDPI(); for (Photo photo : photos) {
if (photo.getImage() == null || photo.getCount() == 0) {
continue;
}
if (photo.getType() == 0) {
photo.getImage().fixImage(6 * DPI_H.x, 4 * DPI_H.y);
this.printImage(photo.getImage().save(), printerNameH, photo.getCount());
} else {
this.printImage(photo.getImage().save(), printerNameL, photo.getCount());
}
}
} private void printUseImage() {
this.initDPI();
this.createTempImage(60, 40);
Printer printerH = findPrinter(printerNameH);
Printer printerL = findPrinter(printerNameL);
GC gcH = new GC(printerH);
GC gcL = new GC(printerL); gcH.setClipping(0, 0, DPI_H.x * 6, DPI_H.y * 4);
gcL.setClipping(0, 0, DPI_H.x * 6, DPI_H.y * 4); boolean flagH = false;
boolean flagL = false;
for (Photo photo : photos) {
if (photo.getImage() == null || photo.getCount() == 0) {
continue;
}
if (photo.getType() == 0) {
if (flagH == false) {
printerH.startJob("photo");
flagH = true;
photo.getImage().getImage(tempImageH);
this.printImage(tempImageH, printerH, gcH, photo.getCount());
}
} else {
if (flagL == false) {
printerL.startJob("photo");
flagL = true;
photo.getImage().getImage(tempImageL);
this.printImage(tempImageL, printerL, gcL, photo.getCount());
}
}
}
if (flagH) {
printerH.endJob();
}
if (flagL) {
printerL.endJob();
}
gcH.dispose();
gcL.dispose();
printerH.dispose();
printerL.dispose();
} private void printUseHDC() {
this.initDPI();
Printer printerH = findPrinter(printerNameH);
Printer printerL = findPrinter(printerNameL);
GC gcH = new GC(printerH);
GC gcL = new GC(printerL);
gcH.setClipping(0, 0, DPI_H.x * 3 + 1, DPI_H.y * 2 + 1);
gcL.setClipping(0, 0, DPI_H.x * 6 + 1, DPI_H.y * 4 + 1); boolean flagH = false;
boolean flagL = false;
for (Photo photo : photos) {
if (photo.getImage() == null || photo.getCount() == 0) {
continue;
}
if (photo.getType() == 0) {
if (flagH == false) {
printerH.startJob("photo");
flagH = true;
int width = 3 * DPI_H.x;
int height = 2 * DPI_H.y;
this.printImage(photo.getImage(), printerH, gcH, width, height, photo.getCount());
}
} else {
if (flagL == false) {
printerL.startJob("photo");
flagL = true;
int width = 6 * DPI_L.x;
int height = 4 * DPI_L.y;
this.printImage(photo.getImage(), printerL, gcL, width, height, photo.getCount());
}
}
}
if (flagH) {
printerH.endJob();
}
if (flagL) {
printerL.endJob();
}
gcH.dispose();
gcL.dispose();
printerH.dispose();
printerL.dispose();
} private void createTempImage(int width, int height) {
try {
tempImageH = new ImageData(width * DPI_H.x / 10, height * DPI_H.y / 10, 32, palette);
tempImageL = new ImageData(width * DPI_L.x / 10, height * DPI_L.y / 10, 32, palette);
System.out.println("h:" + tempImageH.width + "," + tempImageH.height);
System.out.println("l:" + tempImageL.width + "," + tempImageL.height);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
} private void initDPI() {
try {
Printer printerH = findPrinter(printerNameH);
Printer printerL = findPrinter(printerNameL);
DPI_H = printerH.getDPI();
DPI_L = printerL.getDPI();
printerH.dispose();
printerL.dispose();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
} private Printer findPrinter(String name) {
PrinterData[] datas = Printer.getPrinterList(); for (PrinterData data : datas) {
if (data.name.equalsIgnoreCase(name)) {
return new Printer(data);
}
}
return null;
} private void printImage(ImageData data, Printer printer, GC gc, int count) {
Image image = new Image(printer, data);
for (int i = 0; i < count; i++) {
printer.startPage();
gc.drawImage(image, 0, 0);
printer.endPage();
}
image.dispose();
} private void printImage(ImageData data, Printer printer, GC gc, int x, int y) {
Image image = new Image(printer, data);
gc.drawImage(image, x, y);
image.dispose();
} private void printImage(ThumbImage image, Printer printer, GC gc, int width, int height, int count) {
System.out.println("size:" + width + "," + height);
// for(int i = 0;i<count;i++){
// printer.startPage();
image.print(gc.handle, width, height, count);
// printer.endPage();
// }
} private void printImage(String filename, String printerName, int count) {
try {
PrintService service = findPrintService(printerName);
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
FileInputStream stream = new FileInputStream(filename);
Doc doc = new SimpleDoc(stream, flavor, null);
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
for (int i = 0; i < count; i++) {
service.createPrintJob().print(doc, null);
}
} catch (Exception e) {
e.printStackTrace();
}
} private PrintService findPrintService(String printerName) {
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
AttributeSet attributes = new HashAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, attributes);
for (PrintService service : services) {
if (service.getName().equalsIgnoreCase(printerName)) {
return service;
}
}
return null;
}
}
private void printUsePrintService()
是使用打印API
private void printUseHDC()
是使用Win API的DC进行打印
private void printUseImage()
是使用SWT进行绘制是不是打印机的问题啊,我就只有一个打印机,打印机的DPI为720