Skip to content

Instantly share code, notes, and snippets.

@terrancesnyder
Created September 14, 2011 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrancesnyder/1217513 to your computer and use it in GitHub Desktop.
Save terrancesnyder/1217513 to your computer and use it in GitHub Desktop.
Print JPEG Java
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();
}
}
}
@terrancesnyder
Copy link
Author

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.

@SalvatoreCatania
Copy link

HI! Can you tell me where I can find maven code for HaloMaster, Asset and BarcodeDetails? Thanks!

@monchisillo
Copy link

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