exporttiff.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 127 行

JAVA
127
字号

/**
 *    $Id: ExportTIFF.java $
 *
 *    Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved.
 *
 */
package com.jfimagine.jfgraph.transfer;

  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.Color;

import java.io.IOException;
import java.io.Writer;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
        					
//svg, tiff formats
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.GenericDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import org.apache.batik.transcoder.image.*;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;

import com.jfimagine.utils.log.*;

 /**
 * ExportTIFF class is used to output TIFF images.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.3.0 $
 */ 
 
public class ExportTIFF extends ExportBase{

	/**an internal log utility*/
	private JFLogger m_logger=JFLogManager.getLogger(this.getClass());
	
   	/** 
    	*  Export current page or list to a specified file.
    	*  @param fileName A specified file name to be exported.
    	*/
   	public boolean export(String fileName){
		try{ 
			prepareExport(); 

                	int width	=getExportWidth();
                	int height	=getExportHeight();
                
        		// Get a DOMImplementation
        		DOMImplementation domImpl =
            			GenericDOMImplementation.getDOMImplementation();

        		// Create an instance of org.w3c.dom.Document
        		Document document = domImpl.createDocument(null, "svg", null);

        		// Create an instance of the SVG Generator
        		SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        		svgGenerator.setSVGCanvasSize(new Dimension(width,height));

        		//clear graphics.
        		svgGenerator.setColor(Color.white);
        		svgGenerator.fillRect(0, 0, width, height);
		
			draw(svgGenerator);

        		boolean useCSS = true; // we want to use CSS style attribute

			Writer out = new StringWriter();
        		svgGenerator.stream(out,useCSS);
        	        
        	        
			String svgContent   =out.toString();
			out.close();
		

         		//create a TIFF transcoder
         		TIFFTranscoder t = new TIFFTranscoder();
         	
         		//create the input
         		TranscoderInput input = new TranscoderInput(new StringReader(svgContent));
         	
         		// create the transcoder output
         		OutputStream ostream = new FileOutputStream(fileName);
         		TranscoderOutput output = new TranscoderOutput(ostream);
         		// save the image
         		t.transcode(input, output);
         		// flush and close the stream then exit
         		ostream.flush();
         		ostream.close();
       	
        		return true;
		
   		

        	}catch(Exception e){
        		m_logger.error("export:"+e);
        		return false;
        	}
        	
       }
 
   	/** 
    	*  get export file extension.
    	*  @return the export file extension.
    	*/
   	public String getExportFileExt(){
   		return JFFileFilter.FILEEXT_TIFF;
	}

   	/** 
    	*  get export file description
    	*  @return the export file description
    	*/
   	public String getExportFileDesc(){
   		return JFFileFilter.FILEDESC_TIFF;
	}
 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?