Avatar billede svend_bo Nybegynder
04. november 2002 - 16:31

Setting resolution when writing an image - svar gerne på dansk

Svar gerne på dansk. Se evt. http://forum.java.sun.com/thread.jsp?forum=31&thread=319666 for pænere kodeopsætning.

Nuvel, her kommer spørgsmålet:

In a Java application I need to write a tiff-image in a specific resolution, e.g. 300 pixels/inch (dpi), and at size of for example 5 x 2 cm. This implies the pixel-dimension to be approx. 583 x 237 pixels. The tiff-file must be a bitmap, i.e. 1-bit colors (b/w).

My present code is (a modified version of code found at http://access1.sun.com/techarticles/ImagingApps/JImage.html ):

import java.awt.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
import com.sun.media.jai.codec.*;
import javax.media.jai.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.DataBuffer;
import java.awt.geom.GeneralPath;
import java.io.*;
import javax.swing.*;
import java.awt.image.renderable.ParameterBlock;


public class TIFFJai extends JApplet {
   
    public static final String DEFAULT_FILE = "white.tif";
    private static RenderedImage img;
   
    public void init() {
        setBackground(Color.blue);
   
    String fileName = DEFAULT_FILE;
   
    // Read the image from the designated path.
    System.out.println("load image from '" + fileName+"'");
    img =  (RenderedImage)JAI.create("fileload", fileName);
    }


    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
    Dimension d = getSize();
        g2.setBackground(getBackground());
        g2.clearRect(0, 0, d.width, d.height);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                            RenderingHints.VALUE_RENDER_QUALITY);
    int w = d.width;
    int h = d.height;
   
   
    RenderedImageAdapter ria = new RenderedImageAdapter(img);
        BufferedImage bi = ria.getAsBufferedImage();
   
        Graphics2D big = bi.createGraphics();
   
        big.setFont(new Font("Dialog", Font.PLAIN, 10));
        big.setColor(Color.black);
        big.drawString("This is the text!!!", 4, 50);

        g2.drawImage(bi, 0, 0, this);
   


        try {
        File file = new File("images", "test1.tif");
        FileOutputStream out = new FileOutputStream(file);
           
        TIFFEncodeParam params = new TIFFEncodeParam();
        params.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
        ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF",out, params);
        if(encoder == null) {
                System.out.println("imageEncoder is null");
                System.exit(0);
        }
        encoder.encode(bi);
       

        } catch (Exception ex) {
            g2.setColor(Color.red);
            g2.drawString("write permissions on images/test1.tif?", 5, h*2-5);
        }
   
       
    }


    public static void main(String argv[]) {
        final TIFFJai demo = new TIFFJai();
        demo.init();
        JFrame f = new JFrame("JAI Demo - TIFFJai");
        f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        f.getContentPane().add("Center", demo);
        f.pack();
        f.setSize(new Dimension(400,300));
        f.show();
    }
}

The loaded tiff-file 'white.tif' is 300 dpi and 5 x 2 cm and 583 x 237 pixels. But the written tiff-file 'test1.tif' is 72 dpi and 20.6 x 8.3 cm and 583 x 237 pixels. Both have 1-bit colors as needed.

I would very much like 'test1.tif' to have the same measures as 'white.tif'. How is it done?

(It is not a demand that 'white.tif' is loaded as a background. It's only a white-colored image, loaded to get the right dimensions.

A better solution would be to write only the Graphics2D g2 as tiff-file with the right settings.

But how is that done?)

Please write if anything needs to be clarified.

Thanking you all in anticipation - Svend/Birger
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester