Create a blank image object from scratch, annotate it and save it as a tif. : TIF « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Book
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » TIFScreenshots 
Create a blank image object from scratch, annotate it and save it as a tif.
  
//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[] argsthrows 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"};  
        createAnnotatedTifannotateStrings, fout);
        fout.close();
        System.out.println("Completed.");
    }

    //  -----------------------------------------------------------------------------------------------------    
        public void createAnnotatedTif(String[] args, OutputStream outthrows Exception
        {
             
        byte[] byteArray = new byte[] { -1};
        ColorModel colorModel = new IndexColorModel(12, byteArray, byteArray, byteArray);
        
        WritableRaster writeableRaster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1700220011null);
        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(00);  // 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[0new TIFFField(269, TIFFField.TIFF_ASCII, docName.length, docName);
        tiffFields[1new TIFFField(282, TIFFField.TIFF_RATIONAL, 1new long[][] {{ 200} });
        tiffFields[2new TIFFField(283, TIFFField.TIFF_RATIONAL, 1new long[][] { { 200} });
        // resolution unit 
        tiffFields[3new TIFFField(296, TIFFField.TIFF_SHORT, 1new char[] { });
        tiffFields[4new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, SoftwareVersion);  
        
        encodeParam.setExtraFieldstiffFields );
               
        TIFFImageEncoder encoder = new TIFFImageEncoder (out, encodeParam);
        encoder.encode(planarImage);         
    }

}
  

    
  
Related examples in the same category
w__w_w_.___ja___v__a__2___s__.__co__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.