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.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.