为了账号安全,请及时绑定邮箱和手机立即绑定

如何在图像中设置DPI信息?

/ 猿问

如何在图像中设置DPI信息?

拉丁的传说 2019-11-14 15:43:36

我有一个想要导出高分辨率(或更确切地说,是高像素密度?)图像以进行打印的应用程序-例如,我希望以每英寸250点(DPI)的图像而不是默认图像进行打印,据我了解是72 DPI。


我正在使用带有Graphics2D对象的BufferedImage绘制图像,然后使用ImageIO.write()保存图像。


知道如何设置DPI吗?


查看完整描述

3 回答

?
守着星空守着你

Kurt的答案说明了这一点,但我仍然花了一些时间才能运行它,因此这里是保存PNG时设置DPI的代码。要找到合适的作家还有很多事情要做……


 private BufferedImage gridImage;

 ...


 private void saveGridImage(File output) throws IOException {

    output.delete();


    final String formatName = "png";


    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {

       ImageWriter writer = iw.next();

       ImageWriteParam writeParam = writer.getDefaultWriteParam();

       ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

       IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);

       if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {

          continue;

       }


       setDPI(metadata);


       final ImageOutputStream stream = ImageIO.createImageOutputStream(output);

       try {

          writer.setOutput(stream);

          writer.write(metadata, new IIOImage(gridImage, null, metadata), writeParam);

       } finally {

          stream.close();

       }

       break;

    }

 }


 private void setDPI(IIOMetadata metadata) throws IIOInvalidTreeException {


    // for PMG, it's dots per millimeter

    double dotsPerMilli = 1.0 * DPI / 10 / INCH_2_CM;


    IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");

    horiz.setAttribute("value", Double.toString(dotsPerMilli));


    IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");

    vert.setAttribute("value", Double.toString(dotsPerMilli));


    IIOMetadataNode dim = new IIOMetadataNode("Dimension");

    dim.appendChild(horiz);

    dim.appendChild(vert);


    IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");

    root.appendChild(dim);


    metadata.mergeTree("javax_imageio_1.0", root);

 }


查看完整回答
反对 回复 2019-11-14
?
繁花不似锦

设置TIFF DPI

如果要为TIFF设置dpi,请尝试执行以下步骤:


private static IIOMetadata createMetadata(ImageWriter writer, ImageWriteParam writerParams, int resolution) throws

                                                                                                            IIOInvalidTreeException

{

    // Get default metadata from writer

    ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);

    IIOMetadata meta = writer.getDefaultImageMetadata(type, writerParams);


    // Convert default metadata to TIFF metadata

    TIFFDirectory dir = TIFFDirectory.createFromMetadata(meta);


    // Get {X,Y} resolution tags

    BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();

    TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);

    TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);


    // Create {X,Y} resolution fields

    TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } });

    TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } });


    // Add {X,Y} resolution fields to TIFFDirectory

    dir.addTIFFField(fieldXRes);

    dir.addTIFFField(fieldYRes);


    // Add unit field to TIFFDirectory (change to RESOLUTION_UNIT_CENTIMETER if necessary)

    dir.addTIFFField(new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT), BaselineTIFFTagSet.RESOLUTION_UNIT_INCH));


    // Return TIFF metadata so it can be picked up by the IIOImage

    return dir.getAsMetadata();

}

同样,您可以类似的方式设置任何TIFF标签。


查看完整回答
反对 回复 2019-11-14
?
噜噜哒

我在我的项目中将这段代码用于tiff文件,并且效果很好。


import java.awt.image.BufferedImage;

import java.awt.image.RenderedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import javax.imageio.ImageIO;

import javax.media.jai.NullOpImage;

import javax.media.jai.OpImage;

import javax.media.jai.PlanarImage;

import com.sun.media.jai.codec.FileSeekableStream;

import com.sun.media.jai.codec.ImageCodec;

import com.sun.media.jai.codec.ImageDecoder;

import com.sun.media.jai.codec.ImageEncoder;

import com.sun.media.jai.codec.SeekableStream;

import com.sun.media.jai.codec.TIFFEncodeParam;

import com.sun.media.jai.codec.TIFFField;

class SetDDPI

 {

static void tiff_Maker(List<BufferedImage> output, String result) throws   IOException

{

    TIFFEncodeParam params = new TIFFEncodeParam();

    OutputStream out = new FileOutputStream(result);

    List<BufferedImage> imageList = new ArrayList<BufferedImage>();

    for (int i = 1; i < output.size(); i++)

    {

        imageList.add(output.get(i));

    }

    params.setWriteTiled(true);

    params.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);

    params.setExtraImages(imageList.iterator());

    TIFFField[] extras = new TIFFField[2];

    extras[0] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 300, (long) 1 },

            { (long) 0, (long) 0 } });

    extras[1] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 300, (long) 1 },

            { (long) 0, (long) 0 } });

    params.setExtraFields(extras);

    ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);

    encoder.encode(output.get(0));

    out.close();

}

static List<BufferedImage> tiff_Extractor(File tiff) throws IOException

{

    List<BufferedImage> images = new ArrayList<BufferedImage>();

    SeekableStream ss = new FileSeekableStream(tiff);

    ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);

    int numPages = decoder.getNumPages();

    for (int j = 0; j < numPages; j++)

    {

        PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(j), null, null, OpImage.OP_IO_BOUND);

        images.add(op.getAsBufferedImage());


    }

    return images;

}

}

这将设置300 DPI的Tiff图像。您可以根据需要进行更改。


extras[0] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, (Object) new     

long[][] { { (long) 300, (long) 1 },{ (long) 0, (long) 0 } });


extras[1] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, (Object) new     

long[][] { { (long) 300, (long) 1 },{ (long) 0, (long) 0 } });


查看完整回答
反对 回复 2019-11-14

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信