Created
September 14, 2011 19:23
-
-
Save terrancesnyder/1217513 to your computer and use it in GitHub Desktop.
Print JPEG Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.halo.struts.action; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import javax.print.Doc; | |
import javax.print.DocFlavor; | |
import javax.print.DocPrintJob; | |
import javax.print.PrintException; | |
import javax.print.PrintService; | |
import javax.print.PrintServiceLookup; | |
import javax.print.SimpleDoc; | |
import javax.print.attribute.DocAttributeSet; | |
import javax.print.attribute.HashDocAttributeSet; | |
import javax.print.attribute.HashPrintRequestAttributeSet; | |
import javax.print.attribute.PrintRequestAttributeSet; | |
import javax.print.attribute.standard.Copies; | |
import javax.print.attribute.standard.PrintQuality; | |
import javax.print.attribute.standard.PrinterResolution; | |
import com.halo.hibernate.pojo.Asset; | |
import com.halo.hibernate.pojo.BarcodeDetails; | |
import com.halo.hibernate.resources.HaloMaster; | |
import com.halo.resources.Resources; | |
public class PrintImage { | |
static public void main(String args[]) throws Exception { | |
try { | |
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); | |
pras.add(new Copies(1)); | |
pras.add(new PrinterResolution(203,203,PrinterResolution.DPI)); | |
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.JPEG, pras); | |
if (pss.length == 0) | |
throw new RuntimeException("No printer services available."); | |
for(int i=0;i<pss.length;i++) | |
{ | |
Resources.println("pss ["+i+"] = "+pss.toString()); | |
} | |
PrintService ps = pss[0]; | |
System.out.println("Printing to " + ps); | |
DocPrintJob job = ps.createPrintJob(); | |
HaloMaster haloMaster = new HaloMaster(); | |
Asset asset = haloMaster.getAsset(24); | |
BarcodeDetails barcodeDetails = asset.getBarcodeDetails(); | |
String imageSource = ""; | |
if(barcodeDetails != null) | |
imageSource = Resources.getFolderResource("barcodePath")+barcodeDetails.getFileName(); | |
FileInputStream fin = new FileInputStream(imageSource); | |
DocAttributeSet das = new HashDocAttributeSet(); | |
das.add(new PrinterResolution(203,203,PrinterResolution.DPI)); | |
//das.add(new PrintQuality(3)); | |
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.JPEG, das); | |
job.print(doc, pras); | |
fin.close(); | |
} catch (IOException ie) { | |
ie.printStackTrace(); | |
} catch (PrintException pe) { | |
pe.printStackTrace(); | |
} | |
} | |
} |
You can use either of the following to improve the quality of the scaling. I believe BiCubic gives better results but is slower than BILINEAR.
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
I would also not use Image.getScaledInstance() as it is very slow. I'm not sure about the printing as I'm struggling with similar issues.
HI! Can you tell me where I can find maven code for HaloMaster, Asset and BarcodeDetails? Thanks!
I have the same issue as @SalvatoreCatania.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://download.oracle.com/javase/1.4.2/docs/api/javax/print/attribute/standard/PrintQuality.html