View Javadoc

1   package de.desy.video.sw;
2   
3   import java.awt.Graphics;
4   import java.awt.Image;
5   import java.awt.image.BufferedImage;
6   import java.awt.Transparency;
7   //import java.awt.Toolkit;
8   import javax.swing.ImageIcon;
9   import java.awt.GraphicsConfiguration;
10  import java.awt.GraphicsDevice;
11  import java.awt.GraphicsEnvironment;
12  import java.awt.HeadlessException;
13  
14  import java.io.*;
15  import javax.imageio.*;
16  import java.text.DateFormat;
17  import java.text.SimpleDateFormat;
18  
19  //import de.desy.tine.client.*;
20  //import de.desy.tine.dataUtils.*;
21  //import de.desy.tine.definitions.*;
22  import de.desy.tine.types.*;
23  
24  /**
25  * <code>CBasicPNGWriterV3</code> implements saving to of a java.awt.Image to PNG file. 
26  * A header containing metaproperties of the Image is passed additionally in order to  
27  * write them out to an additional .txt file.  
28  * 
29  *    
30  * @author <a href="mailto:stefan.weisse@desy.de">Stefan Weisse</a>
31  * @version $Id: Templates.xml,v 1.10 2008/06/20 16:10:13 sweisse Exp $
32  *
33  */
34  
35  public final class CBasicPNGWriterV3 {
36  
37  	/* NO JAVADOC
38  	 * This method returns a buffered image with the contents of an arbitrary
39  	 * java image.
40  	 */
41  	private static BufferedImage toBufferedImage(Image image) 
42  	{
43  		if (image instanceof BufferedImage) 
44  		{
45  			return (BufferedImage)image;
46  		}
47  
48  		// This code ensures that all the pixels in the image are loaded
49  		image = new ImageIcon(image).getImage();
50  
51  		// Determine if the image has transparent pixels; for this method's
52  		// implementation, see e661 Determining If an Image Has Transparent Pixels
53  		boolean hasAlpha = false;
54  
55  		// Create a buffered image with a format that's compatible with the screen
56  		BufferedImage bimage = null;
57  		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
58  		
59  		try 
60  		{
61  			// Determine the type of transparency of the new buffered image
62  			int transparency = Transparency.OPAQUE;
63  			if (hasAlpha) 
64  			{
65  				transparency = Transparency.BITMASK;
66  			}
67  
68  			// Create the buffered image
69  			GraphicsDevice gs = ge.getDefaultScreenDevice();
70  			GraphicsConfiguration gc = gs.getDefaultConfiguration();
71  			bimage = gc.createCompatibleImage(
72  					image.getWidth(null), image.getHeight(null), transparency);
73  		} 
74  		catch (HeadlessException e) 
75  		{
76  			// The system does not have a screen
77  		}
78  
79  		if (bimage == null) 
80  		{
81  			// Create a buffered image using the default color model
82  			int type = BufferedImage.TYPE_INT_RGB;
83  			if (hasAlpha) 
84  			{
85  				type = BufferedImage.TYPE_INT_ARGB;
86  			}
87  			bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
88  		}
89  
90  		// Copy image to buffered image
91  		Graphics g = bimage.createGraphics();
92  
93  		// Paint the image onto the buffered image
94  		g.drawImage(image, 0, 0, null);
95  		g.dispose();
96  
97  		return bimage;
98  	}
99  
100 	/**
101 	 * convenience overload, accepts filename as String instead of File class
102 	 * 
103 	 * @see write( File, Image, CVideoHeader3)
104 	 */ 
105 	public static boolean write( String aFileName, Image aImg, CVideoHeader3 aHdr)
106 	{
107 		return write(new File(aFileName), aImg, aHdr);
108 	}
109 
110 	/**
111 	 * saves the java.awt.Image passed to a newly created (overwritten) PNG file.
112 	 * Additional metadata describing the Image is taken from a special header and
113 	 * put into an additional textfile placed beside the PNG file.
114 	 * <br>
115 	 * @param aFile java.io.File where to store the png image
116 	 * @param aImg Image data to be written to PNG
117 	 * @param aHdr contains meta properties of Image that will be written to .txt file
118 	 * @return true - image contents and text file were successfully saved<br>
119 	 * 		   false - some error (IOException on png or txt)
120 	 */
121 	public static boolean write( File aFile, Image aImg, CVideoHeader3 aHdr)
122 	{
123 		try 
124 		{
125 			ImageIO.write(toBufferedImage(aImg), "png", aFile);
126 			
127 			
128 			// add .txt header afterwards
129 			String wholename = aFile.getAbsolutePath();
130 			wholename += ".txt";
131 			
132 		    Writer output = null;
133 		    try {
134 		      //use buffering
135 		      //FileWriter always assumes default encoding is OK!
136 		      File txtF = new File(wholename);
137 		      txtF.createNewFile();
138 		      
139 		      String n = System.getProperty("line.separator");
140 		      DateFormat dfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
141 		      
142 		      output = new BufferedWriter( new FileWriter(txtF) );
143 
144 		      IMAGE.SourceHeader srcH = aHdr.sourceHeader;
145 		      IMAGE.FrameHeader  frmH = aHdr.frameHeader;
146 		      
147 		      output.write("SourceHeader."+n);
148 		      output.write(" .version             "+srcH.versionTag+n);
149 		      output.write(" .timestamp           "+dfmt.format(aHdr.getTimestampAsDate())+n);
150 		      output.write(" .cameraport          \""+srcH.cameraPortName+"\""+n);
151 		      output.write(" .total_length        "+srcH.totalLength+n);
152 		      
153 		      output.write(" FrameHeader"+n);
154 		      output.write("       .sourceWidth         	"+frmH.sourceWidth+n);
155 		      output.write("       .sourceHeight        	"+frmH.sourceHeight+n);
156 		      output.write("       .aoiWidth     			"+frmH.aoiWidth+n);
157 		      output.write("       .aoiHeight    			"+frmH.aoiHeight+n);
158 		      output.write("       .xStart       			"+frmH.xStart+n);
159 		      output.write("       .yStart       			"+frmH.yStart+n);
160 		      output.write("       .bytesPerPixel    		"+frmH.bytesPerPixel+n);
161 		      output.write("       .effectiveBitsPerPixel 	"+frmH.effectiveBitsPerPixel+n);
162 		      output.write("       .horizontalBinning   	"+frmH.horizontalBinning+n);
163 		      output.write("       .verticalBinning  		"+frmH.verticalBinning+n);
164 		      output.write("       .sourceFormat    		"+CVideoHeader3.formatToString(frmH.sourceFormat)+n);
165 		      output.write("       .currentFormat    		"+CVideoHeader3.formatToString(frmH.imageFormat)+n);
166 		      output.write("       .frameNumber   			"+frmH.frameNumber+n);
167 		      output.write("       .eventNumer    			"+frmH.eventNumber+n);		      
168 		      output.write("       .xScale       			"+frmH.xScale+n);
169 		      output.write("       .yScale       			"+frmH.yScale+n);
170 		      output.write("       .imageRotation   		"+frmH.imageRotation+n);
171 		      output.write("       .imageFlags   			"+CVideoHeader3.flagsToString(frmH.imageFlags)+n);
172 		      output.write("       .appendedFrameSize 		"+frmH.appendedFrameSize+n);
173 		      output.write("Header END"+n);
174 		    }
175 		    catch( IOException ex1 )
176 		    {
177 		    	return false;
178 		    }
179 		    finally 
180 		    {
181 		      //flush and close both "output" and its underlying FileWriter
182 		      if (output != null) output.close();
183 		    }
184 						
185 		}
186 		catch( IOException ex )
187 		{
188 			return false;
189 		}
190 		
191 		return true;
192 	}
193 	
194 }