Hello please find below some code I wrote to use the jpeg codec which is a standard part of the java 1.3 package. There is also batik (svg) code in there. Hopefully, you'll find this code helpful.
-Brian ----------------------- Brian Gilman <[EMAIL PROTECTED]> Sr. Software Engineer MIT/Whitehead Inst. Center for Genome Research One Kendall Square, Bldg. 300 / Cambridge, MA 02139-1561 USA phone +1 617 252 1069 / fax +1 617 252 1902 On Sat, 12 Jan 2002, Mark Schreiber wrote: > Hi I have been meaning to post this for a while, It is some source for a > logoPainter that I made for an upcomming bioinformatics paper regarding > the correction for signals in a biased genome. > > It is based on a Biojava-live (although perhaps a slightly earlier > release so it made need the odd tweak) Anyhow it demonstrates some writing > to Jpeg Tiff bmp etc. Uses the Java Advanced Image Library (JAI 1.1 from > Sun). Please feel free to use / review the code as you see fit. Perhaps > ask permission before selling it ;) > > I would be happy for it to be made part of the CVS if people want. It > could probably do with some peer review so to speak as it has a few rough > edges in the code that others might be able to make more elegant. > > Enjoy > > Mark > > > > On Fri, 11 Jan 2002, Bert Coessens wrote: > > > Dear all, > > > > Does anybody know how to write a graphics object to a jpeg file? I > > already tried almost everything but still with no result. The code > > beneath is what I think should work, but if I run it, I only get a black > > image of 300 by 300. Can anybody tell me what I'm doing wrong? > > > > * variable dist is some distance > > * sLogo is not a null object, I can visualise it inside a JFrame object > > > > code: > > { > > DistributionLogo sLogo = new DistributionLogo(); > > TextLogoPainter logoPainter = new TextLogoPainter(); > > sLogo.setLogoPainter(logoPainter); > > sLogo.setStyle(new DNAStyle()); > > sLogo.setDistribution(dist); > > > > BufferedImage bufIm = new BufferedImage(300, 300, > > BufferedImage.TYPE_INT_RGB); > > Graphics g = bufIm.createGraphics(); > > logoPainter.paintLogo(g, sLogo); > > > > File outfile = new File("motif.jpeg"); > > FileOutputStream outstream = new FileOutputStream(outfile); > > com.sun.image.codec.jpeg.JPEGImageEncoder encoder = > > > > com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(outstream); > > encoder.encode(img); > > } > > > > Thanks in advance!! > > Bert > > > > _______________________________________________ > > Biojava-l mailing list - [EMAIL PROTECTED] > > http://biojava.org/mailman/listinfo/biojava-l > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Mark Schreiber Ph: 64 3 4797875 > Rm 218 email [EMAIL PROTECTED] > Department of Biochemistry > University of Otago > PO Box 56 > Dunedin > New Zealand > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >
package org.ensembl.draw; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; /******************* * For SVG Generation * Added 12/08/01 *******************/ import org.apache.batik.svggen.*; import org.apache.batik.dom.*; import org.w3c.dom.*; import org.apache.batik.transcoder.*; import org.apache.batik.transcoder.image.*; import org.apache.batik.bridge.*; import org.apache.fop.svg.*; import org.apache.fop.*; import com.sun.image.codec.jpeg.*; /* * WHITEHEAD INSTITUTE * SOFTWARE COPYRIGHT NOTICE AGREEMENT * This software and its documentation are copyright 2001 by the * Whitehead Institute for Biomedical Research. All rights are reserved. * * This software is supplied without any warranty or guaranteed support * whatsoever. The Whitehead Institute can not be responsible for its * use, misuse, or functionality. * * We would like to thank Ewan Birney and the Ensembl team at the Sanger Centre * for providing the original source code which was used as a template for this * API and code base. * *@author John Mayer and the OmniGene team */ public class ContigView extends JPanel { public ChromPane chromPane; public OverviewPane overviewPane; public DetailedPane detailedPane; int width = 800; OmniView appFrame; ContigView(OmniView appFrame) { this.appFrame = appFrame; setLayout(null); setBackground(Color.white); detailedPane = new DetailedPane(appFrame,this); chromPane = new ChromPane(appFrame,this,detailedPane.selPanel); overviewPane = new OverviewPane(appFrame,this,detailedPane.selPanel); detailedPane.refreshConfig(); position(); PopupManager popMan = new PopupManager(this); addMouseListener(popMan); addMouseMotionListener(popMan); JMenuBar mbar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); JMenuItem exitItem = new JMenuItem("Exit"); JMenuItem optionItem = new JMenuItem("Options"); JMenu exportItem = new JMenu("Export..."); JMenuItem asSVG = new JMenuItem("As SVG"); JMenuItem asPDF = new JMenuItem("As PDF"); JMenuItem asJPEG =new JMenuItem("As JPEG"); optionItem.addActionListener(new ConfigurationHandler(appFrame, this) ); asSVG.addActionListener(new ExportHandler(this, "svg")); asPDF.addActionListener(new ExportHandler(this,"pdf")); asJPEG.addActionListener(new ExportHandler(this,"jpeg")); appFrame.setJMenuBar(mbar); mbar.add(fileMenu); mbar.add(editMenu); fileMenu.add(exportItem); fileMenu.add(exitItem); editMenu.add(optionItem); exportItem.add(asSVG); exportItem.add(asPDF); exportItem.add(asJPEG); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }); } void position() { Dimension chromDim = chromPane.plannedSize(), overviewDim = overviewPane.plannedSize(), detailedDim = detailedPane.plannedSize(); int overviewTop = chromDim.height; int detailedviewTop = overviewTop+overviewDim.height; chromPane.position(0,0,width,chromDim.height); overviewPane.position(0,overviewTop,width,overviewDim.height); detailedPane.position(0,detailedviewTop,width,detailedDim.height); } public Pane paneIncludingPoint(int x, int y) { Dimension chromDim = chromPane.plannedSize(), overviewDim = overviewPane.plannedSize(), detailedDim = detailedPane.plannedSize(); int overviewTop = chromDim.height; int detailedviewTop = overviewTop+overviewDim.height; if (new Rectangle(0,0,width,chromDim.height).contains(x,y)) return chromPane; if (new Rectangle(0,overviewTop,width,overviewDim.height).contains(x,y)) return overviewPane; if (new Rectangle(0,detailedviewTop,width,detailedDim.height).contains(x,y)) return detailedPane; return null; } BufferedImage bufferImage; public void paintComponent(Graphics g) { super.paintComponent(g); if (plannedSize().height == 0) return; bufferImage = new BufferedImage(width,plannedSize().height, BufferedImage.TYPE_INT_RGB); Graphics bufferg = bufferImage.getGraphics(); super.paintComponent(bufferg); chromPane.paint(bufferg); overviewPane.paint(bufferg); detailedPane.paint(bufferg); g.drawImage(bufferImage,0,0,this); } public Dimension getPreferredSize() { return plannedSize(); } Dimension plannedSize() { int plannedHeight = chromPane.plannedSize().height + overviewPane.plannedSize().height + detailedPane.plannedSize().height; return new Dimension(800,plannedHeight); } public void exportAsSVG() throws IOException, UnsupportedEncodingException,FileNotFoundException{ //Get an instance of a DOM tree to represent the graphics in this pane DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); //Get an instance document Document doc = domImpl.createDocument(null, "svg", null); //Create an instance of an SVG Generator SVGGraphics2D svgGen = new SVGGraphics2D(doc); //send the genetator to our paint routine this.paintComponent(svgGen); //now stream this all out as SVG boolean useCSS = true; Writer out = new OutputStreamWriter(new FileOutputStream("./Output.svg"), "UTF-8"); svgGen.stream(out,useCSS); out.close(); } public void exportAsJPEG() throws IOException,FileNotFoundException{ OutputStream out = new FileOutputStream("Output.jpeg"); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bufferImage); out.flush(); out.close(); } public void exportAsPDF() throws IOException, UnsupportedEncodingException,FileNotFoundException,TranscoderException{ //first export this as an SVG exportAsSVG(); //now get in instance of a JPEG transcoder PDFTranscoder t = new PDFTranscoder(); //set some transcoding hints... //t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8)); //t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Integer(100)); //get the SVGFile we created TranscoderInput input = new TranscoderInput(new File("Output.svg").toURL().toString()); //Create an output OutputStream ostream = new FileOutputStream("Output.pdf"); TranscoderOutput output = new TranscoderOutput(ostream); //save the image t.transcode(input, output); //flush everything down the pipe ostream.flush(); ostream.close(); } }