//Code by Morrow, Joe [GCG-PFS] (Joe.Morrow at Primerica.com)
/*
I've created a sample class that will create an image object,
annotate it and save it as a tif file.
This should be a good example for you to post.
I've needed to do this for a long time, and found no good examples
of how to do it. Also the documentation on the JAI is somewhat
lacking especially when it comes to the proper format and use of
tif tags, and how and when to update them. This sample will be
useable by almost all tif viewers / printers, something that is
sorely lacking in many other examples.
Joe Morrow
*/
package image.text;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
// used jai-1_1_4\lib\ext\jai_core.jar
import javax.media.jai.PlanarImage;
// used jai_codec-1.1.3-alpha.jar
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFField;
import com.sun.media.jai.codecimpl.TIFFImageEncoder;
/**
* Create a blank image object from scratch, annotate it and save it as a tif.
* The tif should be viewable by many tif viewers.
* It's 8.5 x 11 inches, at 200 dpi resolution. I.E. Initially created with size of 1700 x 2200.
*/
public class CreateTifAndAnnotate
{
public static void main(String[] args)
{
CreateTifAndAnnotate me = new CreateTifAndAnnotate();
try
{
me.runMe(args);
} catch (Exception e)
{
e.printStackTrace();
}
}
// -----------------------------------------------------------------------------------------------------
private void runMe(String[] args) throws Exception
{
File myFile = new File("C:\\temp\\AnnotatedTif.tif");
if (myFile.exists())
myFile.delete();
FileOutputStream fout = new FileOutputStream(myFile);
String annotateStrings[] = new String[] {"This is the first annotation","Some more annotation","and a third line"};
createAnnotatedTif( annotateStrings, fout);
fout.close();
System.out.println("Completed.");
}
// -----------------------------------------------------------------------------------------------------
public void createAnnotatedTif(String[] args, OutputStream out) throws Exception
{
byte[] byteArray = new byte[] { -1, 0 };
ColorModel colorModel = new IndexColorModel(1, 2, byteArray, byteArray, byteArray);
WritableRaster writeableRaster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1700, 2200, 1, 1, null);
BufferedImage bufImg = new BufferedImage(colorModel, writeableRaster, false, null);
// -------------------------------------------------------------------
Graphics2D g2d = bufImg.createGraphics() ;
g2d.setColor ( Color.black ) ;
Font font = new Font("Arial Bold", Font.PLAIN, 36);
g2d.setFont(font);
int vertPos = 200;
for (int i=0; i<args.length; i++)
{
g2d.drawString ( args[i], 75, vertPos ) ;
vertPos += 48;
}
PlanarImage planarImage = PlanarImage.wrapRenderedImage ( bufImg ) ;
TIFFEncodeParam encodeParam = new TIFFEncodeParam ();
encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
encodeParam.setWriteTiled(false); // false means use strips.
encodeParam.setTileSize(0, 0); // tiling will make the file size much larger.
encodeParam.setLittleEndian(true); // create an Intel (II) format image
String SoftwareVersion[] = new String[] {"TIFF by Joe, " + this.getClass().getName()};
String docName[] = new String[] {"JoesTifAnnotator"};
// Create a new TIFF fields including a new TIFF ASCII TIFF tag.
TIFFField tiffFields[] = new TIFFField[5];
tiffFields[0] = new TIFFField(269, TIFFField.TIFF_ASCII, docName.length, docName);
tiffFields[1] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, new long[][] {{ 200, 1 } });
tiffFields[2] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
// resolution unit
tiffFields[3] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, new char[] { 2 });
tiffFields[4] = new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, SoftwareVersion);
encodeParam.setExtraFields( tiffFields );
TIFFImageEncoder encoder = new TIFFImageEncoder (out, encodeParam);
encoder.encode(planarImage);
}
}
|