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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 2D Graphics GUI » TIF 




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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.