Skip to main content

How to create image with higher quality(300dpi) using JAVA?

2 replies [Last post]
navin_162
Offline
Joined: 2003-10-08

I have created an image using java with some text and other geometrical objects in it. The output of this image is 96dpi by default. The clarity of the image is not sufficient. So i want to create an image with a better clarity and sharpness. I would want to retain the size of the image generated.

Please let me know how I can create the image with 300 DPI.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mihir_amreli
Offline
Joined: 2006-07-31

Hello

did you find any solution ? i am exactly looking for the same thing.

navin_162
Offline
Joined: 2003-10-08

The code I have implemented is:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

import java.io.File;
import java.io.IOException;

import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageOutputStream;

import designer.barcode.generators.JBarcodeBean;

public class testImageDPI {
public testImageDPI() {
super();
}

public static void main(String[] args) {
testImageDPI objDPI = new testImageDPI();
objDPI.drawObjects();
}

public void drawObjects() {
JBarcodeBean jbbBarcode;
try {
Rectangle rectCoveringArea = new Rectangle(0, 0, 250, 250);

int STOCK_X = rectCoveringArea.width;
int STOCK_Y = rectCoveringArea.height;
BufferedImage image =
new BufferedImage((int)rectCoveringArea.getWidth(),
(int)rectCoveringArea.getHeight(), 1);
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
Color c = new Color(Integer.parseInt("123456", 16));
g2d.fillRect(0, 0, (int)rectCoveringArea.getWidth(),
(int)rectCoveringArea.getHeight());
g2d.setColor(c);

c = new Color(0x000000);
//g2d.setColor(c);

BasicStroke bs = new BasicStroke(2);
g2d.setStroke(bs);
g2d.drawRect(0, 0, STOCK_X, STOCK_Y);

c = new Color(0x000000);
//g2d.setColor(c);
bs = new BasicStroke(1.7f);
g2d.setStroke(bs);
g2d.setFont(new Font("Tahoma", Font.PLAIN, 20));
g2d.drawString("TEST STRING", 60, 60);

g2d.drawLine(50, 50, 10, 10);

saveGridImage(new File("d:\\testDPI.png"), image);
} catch (Exception e) {
e.printStackTrace();
}
}

public void saveGridImage(File output,
RenderedImage image) throws IOException {

final String formatName = "png";

for (Iterator iw = ImageIO.getImageWritersByFormatName(formatName);
iw.hasNext(); ) {
ImageWriter writer = (ImageWriter)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;
}
final ImageOutputStream stream =
ImageIO.createImageOutputStream(output);
try {
writer.setOutput(stream);
writer.write(metadata, new IIOImage(image, null, metadata),
writeParam);

} finally {
stream.close();
writer.reset();
}
break;
}
}
}