gray-scale image to matrix and matrix to gray-scale image - java

I am trying to get 2D matrix from gray-scale image and modify it, then return back to the gray-scale image.
But when try to convert the image to a matrix, the pixels return with black values.
How can I find a solution for this problem?
You can find the images below the code
Java code:
public class MyImageProcessing {
private SampleModel sampleModel;
public int[][] compute(File file)
{
try
{
BufferedImage img= ImageIO.read(file);
Raster raster=img.getData();
sampleModel = raster.getSampleModel();
int w=raster.getWidth(),h=raster.getHeight();
int pixels[][]=new int[w][h];
for (int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
pixels[x][y]=raster.getSample(x,y,0);
}
}
return pixels;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public void getImage(int pixels[][])
{
int w=pixels.length;
int h=pixels[0].length;
WritableRaster raster= Raster.createWritableRaster(sampleModel, new Point(0,0));
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
raster.setSample(i,j,0,pixels[i][j]);
}
}
BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
image.setData(raster);
File output=new File("C:\\Users\\salam\\Pictures\\3.png");
try {
ImageIO.write(image,"png",output);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Original image:
Image retrieved from matrix:

Try this code out.
If it is not what you need then perhaps what you could take away from my post is that code should always be neat, readable, properly indented, and commented, all of which adds to the understandability of the program.
Note: Your image returns practically the same black and white image when converted to grayscale, because your image is black and white much like a binary image.
I can also suggest OpenCV (https://opencv.org/) which is under a BSD licence, it's simple, powerful, and available for Java - although I remember it being a PITA when installing it on Ubuntu 16.04.
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* This class converts images to grayscale color.
*/
public class GrayscaleConverter {
/**
* Creates a new grayscaled BufferedImage object from the given source image
* by averaging each pixels RGB value.
*
* #param inputImageAbsPath the absolute path of the image file, including its name and extension.
* #return a BufferedImage object.
*/
private BufferedImage compute(String inputImageAbsPath) {
System.out.println("... Converting source image to gray scale.");
BufferedImage img = null; // image file
// Read the source image or throw an exception
try {
img = ImageIO.read(new File(inputImageAbsPath));
} catch(Exception e) {
e.printStackTrace();
}
// Get the image width and height dimensions
int width = img.getWidth();
int height = img.getHeight();
// Convert to grayscale by looping over pixels, beginning at top-most left coordinate (0,0)
for (int y = 0; y < height; y++) { // y = rows
for (int x = 0; x < width; x++) { // x = columns
// Get the pixel value at this (x,y) coordinate
int p = img.getRGB(x,y);
// Extract the alpha, R, G, B values from pixel p
int a = (p>>24) & 0xff; // Shift bits and unsign
int r = (p>>16) & 0xff;
int g = (p>>8) & 0xff;
int b = p & 0xff;
// Calculate average color (grayscale it)
int avg = (r+g+b)/3;
// Replace RGB value with avg
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
img.setRGB(x, y, p);
}
}
return img;
}
/**
* Saves the converted grayscale image. This method builds the save path from the provided file name,
* file extension, and absolute path of the folder that you want to save the image in.
*
* #param path the absolute path of the folder that you would like to save the image inside.
* #param imageName the name you would like to save the image with.
* #param imageFileType the image file extension, without the dot (.) preceding the image file type.
* #param image the BufferedImage object returned from the compute method.
*/
private void saveImage(String path, String imageName, String imageFileType, BufferedImage image) {
// Save or throw exception
try {
System.out.println("... Saving grayscale image to "
+ path.concat("\\").concat(imageName).concat(".").concat(imageFileType)); // save path displayed to user
ImageIO.write(image,
imageFileType,
new File(path.concat("\\").concat(imageName).concat(".").concat(imageFileType)));
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("... Image saved.");
}
// Driver
public static void main(String args[]) throws IOException {
/*
* Tested for .png and .jpg files. Both worked successfully.
*/
// Test
System.out.println("Testing GrayscaleConverter.\n");
String input = "*source images absolute file path including name and extension*";
String outputPath = "*absolute path to folder where you will save grayscale image in*";
String outputFileName = "*save image with this name*";
String outputFileType = "*save image with this file extension (no dot (.) e.g. png or jpg)*";
GrayscaleConverter gsc = new GrayscaleConverter();
BufferedImage convertedImage = gsc.compute(input);
gsc.saveImage(outputPath, outputFileName, outputFileType, convertedImage );
System.out.println("\nTest complete.");
}
}
Your supplied input image:
Your output image:
Another sample input image:
Another sample output image:
I tested my program with both .png and .jpg image files and it worked. Good luck.

Related

Improving image quality for Tesseract Tess4j?

On the website where I screenshoted the image the numbers will change continuously.
What I'm trying to do is to read the image text(which is numbers, dot and X on the image) using Tesseract, tess4j java.
The problem is I'm getting inconsistent results, sometimes I get letters sometimes letters with numbers.
After I blacklisted letters excerpts the letter X and special character . I now get 4.0 if I'm not getting the correct results from the picture.
I added a code below to GrayScale the image but still I'm getting the same inconsistent results.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class GrayScalingImage {
public static void main(String args[]) throws Exception {
try {
File inputImage = new File("image.jpg"); BufferedImage image = ImageIO.read(inputImage);
for(int i=0; i<image.getHeight(); i++) {
for(int j=0; j<image.getWidth(); j++) {
Color color = new Color(image.getRGB(j, i));
int red = (int)(color.getRed() * 0.299);
int green = (int)(color.getGreen() * 0.587);
int blue = (int)(color.getBlue() * 0.114);
Color newColor = new Color(red+green+blue, red+green+blue,red+green+blue);
image.setRGB(j, i, newColor.getRGB());
}
}
File ouptut = new File("newImage.jpg"); ImageIO.write(image, "jpg", ouptut);
}
catch (Exception e) {
}
}
}

Negative X or Y obtained from PdfBox while extracting text position

I am trying to extract all text in a pdf along with their coordinates.
I am using Apache PDFBox 2.0.8 and following the sample program DrawPrintTextLocations .
It seems to work mostly, but for certain pdf-s i get negative values for the x and y coordinates of the bounding boxes. Refer this pdf file for example.
My app assumes the coordinate system as a normal pdf (x goes from left to right an y goes top to bottom). so these are throwing my computations off.
Below is the relevant piece of code.
import org.apache.fontbox.util.BoundingBox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.pdmodel.interactive.pagenavigation.PDThreadBead;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
/**
* This is an example on how to get some x/y coordinates of text and to show them in a rendered
* image.
*
* #author Ben Litchfield
* #author Tilman Hausherr
*/
public class DrawPrintTextLocations extends PDFTextStripper {
private AffineTransform flipAT;
private AffineTransform rotateAT;
private AffineTransform transAT;
private final float DPI = 200.0f;
private final double PT2PX = DPI / 72.0;
private final AffineTransform dpiAT = AffineTransform.getScaleInstance(PT2PX, PT2PX);
private final String filename;
static final int SCALE = 1;
private Graphics2D g2d;
private final PDDocument document;
/**
* Instantiate a new PDFTextStripper object.
*
* #param document
* #param filename
* #throws IOException If there is an error loading the properties.
*/
public DrawPrintTextLocations(PDDocument document, String filename) throws IOException {
this.document = document;
this.filename = filename;
}
/**
* This will print the documents data.
*
* #param args The command line arguments.
* #throws IOException If there is an error parsing the document.
*/
public static void main(String[] args) throws IOException {
String pdfLoc = "/debug/pdfbox/p2_VS008PI.pdf";
if (args.length == 1) {
pdfLoc = args[0];
}
try (PDDocument document = PDDocument.load(new File(pdfLoc))) {
DrawPrintTextLocations stripper = new DrawPrintTextLocations(document, pdfLoc);
stripper.setSortByPosition(true);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
stripper.stripPage(page);
}
}
}
private void stripPage(int page) throws IOException {
PDFRenderer pdfRenderer = new PDFRenderer(document);
BufferedImage image = pdfRenderer.renderImageWithDPI(page, DPI);
PDPage pdPage = document.getPage(page);
PDRectangle cropBox = pdPage.getCropBox();
// flip y-axis
flipAT = new AffineTransform();
flipAT.translate(0, pdPage.getBBox().getHeight());
flipAT.scale(1, -1);
// page may be rotated
rotateAT = new AffineTransform();
int rotation = pdPage.getRotation();
if (rotation != 0) {
PDRectangle mediaBox = pdPage.getMediaBox();
switch (rotation) {
case 90:
rotateAT.translate(mediaBox.getHeight(), 0);
break;
case 270:
rotateAT.translate(0, mediaBox.getWidth());
break;
case 180:
rotateAT.translate(mediaBox.getWidth(), mediaBox.getHeight());
break;
default:
break;
}
rotateAT.rotate(Math.toRadians(rotation));
}
// cropbox
transAT = AffineTransform.getTranslateInstance(-cropBox.getLowerLeftX(), cropBox.getLowerLeftY());
g2d = image.createGraphics();
g2d.setStroke(new BasicStroke(0.1f));
g2d.scale(SCALE, SCALE);
setStartPage(page + 1);
setEndPage(page + 1);
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
writeText(document, dummy);
g2d.dispose();
String imageFilename = filename;
int pt = imageFilename.lastIndexOf('.');
imageFilename = imageFilename.substring(0, pt) + "-marked-" + (page + 1) + ".png";
ImageIO.write(image, "png", new File(imageFilename));
}
/**
* Override the default functionality of PDFTextStripper.
*/
#Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
for (TextPosition text : textPositions) {
AffineTransform at = text.getTextMatrix().createAffineTransform();
PDFont font = text.getFont();
BoundingBox bbox = font.getBoundingBox();
float xadvance = font.getWidth(text.getCharacterCodes()[0]); // todo: should iterate all chars
Rectangle2D.Float rect1 = new Rectangle2D.Float(0, bbox.getLowerLeftY(), xadvance, bbox.getHeight());
if (font instanceof PDType3Font) {
at.concatenate(font.getFontMatrix().createAffineTransform());
} else {
at.scale(1 / 1000f, 1 / 1000f);
}
Shape s1 = at.createTransformedShape(rect1);
s1 = flipAT.createTransformedShape(s1);
s1 = rotateAT.createTransformedShape(s1);
s1 = dpiAT.createTransformedShape(s1);
g2d.setColor(Color.blue);
g2d.draw(s1);
Rectangle bounds = s1.getBounds();
if (bounds.getX() < 0 || bounds.getY() < 0) {
// THIS is where things go wrong
// i need these coordinates to be +ve
System.out.println(bounds.toString());
System.out.println(rect1.toString());
}
}
}
}
And here is some snippet of the output from the first page of the above pdf.
SECTION 10 – INSURANCE & OTHER FINANCIAL RESOURCES
java.awt.Rectangle[x=-3237,y=40,width=19,height=43]
java.awt.Rectangle[x=-3216,y=40,width=20,height=43]
java.awt.Rectangle[x=-3194,y=40,width=23,height=43]
java.awt.Rectangle[x=-3170,y=40,width=22,height=43]
The characters with negative coordinates are outside the cropbox (also characters with coordinates bigger than the cropbox height / width). See the cropbox as a cutout from something bigger. To see the whole thing, run this code
pdPage.setCropBox(pdPage.getMediaBox());
for each page of your PDF and then save and view it.
Per your comment
Following your advice of setting the crop box to the media box, actually changed the whole on screen appearance of the pdf, now i got 3 pages collated as one.
This suggests that physically, this is a folded sheet that has 3 pages on each side. The online PDF displays this as 6 pages for easy viewing on a computer.

Image input output in Java

In this code I have taken an image as input and output the same image. As far I know if two images are same then their PSNR value will be inf. So I calculate their PSNR value using MATLAB but it shows 48.05 that means those image are not same. But I read and write the same image, why this is happening. How can I fix it?
public class ImageProcessing {
BufferedImage image = null;
int width;
int height;
public ImageProcessing() {
// Input the image
try {
File input = new File("image0.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
/*int count = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
count++;
Color c = new Color(image.getRGB(j, i));
System.out.println("S.No: " + count + " Red: " + c.getRed() + " Green: " + c.getGreen() + " Blue: " + c.getBlue());
}
}*/
} catch (Exception e) {
System.out.println("Error: " + e);
}
// Output the image
try {
File input = new File("image1.jpg");
ImageIO.write(image, "jpg", input);
System.out.println("Writing complete.");
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
public static void main(String[] args) {
// TODO code application logic here
System.out.println("System Start");
ImageProcessing obj = new ImageProcessing();
}
}
JPG is a lossy format, You will lose information reading it in and writing it out each time you save it.
Perhaps try using a non-lossy format such as PNG or GIF.
Right, to go off of Salmans comment, with lossless compression, every bit of data originally in the file remains after its uncompressed. This is generally used for txt documents or spreadsheet files where information can be lost such as financial data. PNG or GIF use lossless compression.
What would be the advantage of using loss compression? It makes the file smaller by removing redundant information. Typically the user doesn't notice it and is used in sound and video. JPEG uses lossy compression and usually the creator can decide how much loss to introduce to trade-off between file size and image quality.
Drawn mostly from: http://whatis.techtarget.com/definition/lossless-and-lossy-compression

Image size returns -1 in JAR but proper size in IDE. Image loaded via Toolkit....createImage(url)

Intention: Get BufferedImage from resource (be it in IDE or running JAR).
Problem: Getting an Image always works, converting to BufferedImage requires knowledge of size, but size always returns -1, even after waiting with MediaTracker. In IDE, size after MediaTracker is proper, before it is -1. Waiting with while-loop until size >-1 seems to never end in running JAR. (Tried with code language level 6 and 8 using JDK/JRE 8.)
Output of SSCCE in IDE:
IMAGE: sun.awt.image.ToolkitImage#15975490
WIDTH: -1
DURATION: 38 ms
WIDTH: 32
BUFFEREDIMAGE: BufferedImage#6adede5: type = 1 DirectColorModel:
rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster:
width = 32 height = 32 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0
EDIT: MediaTracker's "isErrorAny()" returns false.
Output of SSCCE in running JAR:
IMAGE: sun.awt.image.ToolkitImage#2a84aee7
WIDTH: -1
DURATION: 23 ms
WIDTH: -1
Exception in thread "main"
java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot
be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown
Source)
at java.awt.image.BufferedImage.(Unknown Source)
at ImageSizeProblem.createBufferedImageFromImage(ImageSizeProblem.java:82)
at ImageSizeProblem.main(ImageSizeProblem.java:26)
EDIT: MediaTracker's "isErrorAny()" returns true. But I have no way of finding out what the error is - also, the image does load properly, since it can be successfully used in an JToolBar via making an ImageIcon from it. (I already tried abusing ImageIcon's getIconWidth() or getImage() methods - they bring no improvement at all.)
SSCCE:
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
public class ImageSizeProblem {
public static void main(final String[] args) {
final Image img = createImageFromResource("test.png");
System.out.println("IMAGE: " + img);
System.out.println("WIDTH: " + img.getWidth(null));
final long startTime = System.currentTimeMillis();
waitForImage(img);
final long duration = System.currentTimeMillis() - startTime;
System.out.println("\nDURATION: " + duration + " ms");
System.out.println("\nWIDTH: " + img.getWidth(null));
final BufferedImage buffImg = createBufferedImageFromImage(img, false);
System.out.println("\nBUFFEREDIMAGE: " + buffImg);
System.exit(0);
}
private static Image createImageFromResource(final String resourceFileName_dontForgetToAddItsFolderToClasspath) {
final Toolkit kit = Toolkit.getDefaultToolkit();
final URL url = ClassLoader.getSystemResource(resourceFileName_dontForgetToAddItsFolderToClasspath);
if (url != null) {
final Image img = kit.createImage(url);
if (img == null) {
System.err.println("Image resource could not be loaded.");
return null;
}
return img;
} else {
System.err.println("Image resource not found.");
return null;
}
}
private static boolean waitForImage(final Image img) {
final MediaTracker mediaTracker = new MediaTracker(new JPanel());
mediaTracker.addImage(img, 1);
try {
mediaTracker.waitForID(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
return !mediaTracker.isErrorAny();
}
// improved version of http://stackoverflow.com/a/13605411/3500521
private static BufferedImage createBufferedImageFromImage(final Image img, final boolean withTransparency) {
if (img instanceof BufferedImage) {
return (BufferedImage) img;
} else if (img == null) {
return null;
}
final int w = img.getWidth(null);
final int h = img.getWidth(null);
final BufferedImage bufferedImage;
if (withTransparency) {
bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
} else {
bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}
final Graphics2D g = bufferedImage.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bufferedImage;
}
}
Note that the image used for testing was loaded and used successfully in another application via new ImageIcon(image) in a JToolBar. Getting the size from the ImageIcon also returns -1, also from the image returned by ImageIcon.getImage().
The problem was path depth.
I also tried Avira, because the problem is technically a program accessing its own file, which could well seem suspicious, but while Avira was uninstalling, I tried the executables in a different location with a shorter path, and everything worked like a charm.

java setting resolution and print size for an Image

I wrote a program that generates a BufferedImage to be displayed on the screen and then printed. Part of the image includes grid lines that are 1 pixel wide. That is, the line is 1 pixel, with about 10 pixels between lines. Because of screen resolution, the image is displayed much bigger than that, with several pixels for each line. I'd like to draw it smaller, but when I scale the image (either by using Image.getScaledInstance or Graphics2D.scale), I lose significant amounts of detail.
I'd like to print the image as well, and am dealing with the same problem. In that case, I am using this code to set the resolution:
HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PrinterResolution pr = new PrinterResolution(250, 250, ResolutionSyntax.DPI);
set.add(pr);
job.print(set);
which works to make the image smaller without losing detail. But the problem is that the image is cut off at the same boundary as if I hadn't set the resolution. I'm also confused because I expected a larger number of DPI to make a smaller image, but it's working the other way.
I'm using java 1.6 on Windows 7 with eclipse.
Regarding the image being cut-off on the page boundary, have you checked the clip region of the graphics? I mean try :
System.out.println(graphics.getClipBounds());
and make sure it is correctly set.
I had the same problem. Here is my solution.
First change the resolution of the print job...
PrinterJob job = PrinterJob.getPrinterJob();
// Create the paper size of our preference
double cmPx300 = 300.0 / 2.54;
Paper paper = new Paper();
paper.setSize(21.3 * cmPx300, 29.7 * cmPx300);
paper.setImageableArea(0, 0, 21.3 * cmPx300, 29.7 * cmPx300);
PageFormat format = new PageFormat();
format.setPaper(paper);
// Assign a new print renderer and the paper size of our choice !
job.setPrintable(new PrintReport(), format);
if (job.printDialog()) {
try {
HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PrinterResolution pr = new PrinterResolution((int) (dpi), (int) (dpi), ResolutionSyntax.DPI);
set.add(pr);
job.setJobName("Jobname");
job.print(set);
} catch (PrinterException e) {
}
}
Now you can draw everything you like into the new high resolution paper like this !
public class PrintReport implements Printable {
#Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
// Convert pixels to cm to lay yor page easy on the paper...
double cmPx = dpi / 2.54;
Graphics2D g2 = (Graphics2D) g;
int totalPages = 2; // calculate the total pages you have...
if (page < totalPages) {
// Draw Page Header
try {
BufferedImage image = ImageIO.read(ClassLoader.getSystemResource(imgFolder + "largeImage.png"));
g2.drawImage(image.getScaledInstance((int) (4.8 * cmPx), -1, BufferedImage.SCALE_SMOOTH), (int) (cmPx),
(int) (cmPx), null);
} catch (IOException e) {
}
// Draw your page as you like...
// End of Page
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
It sounds like your problem is that you are making the grid lines part of the BufferedImage and it doesn't look good when scaled. Why not use drawLine() to produce the grid after your image has been drawn?
Code for Convert image with dimensions using Java and print the converted image.
Class: ConvertImageWithDimensionsAndPrint.java
package com.test.convert;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ConvertImageWithDimensionsAndPrint {
private static final int IMAGE_WIDTH = 800;
private static final int IMAGE_HEIGHT = 1000;
public static void main(String[] args) {
try {
String sourceDir = "C:/Images/04-Request-Headers_1.png";
File sourceFile = new File(sourceDir);
String destinationDir = "C:/Images/ConvertedImages/";//Converted images save here
File destinationFile = new File(destinationDir);
if (!destinationFile.exists()) {
destinationFile.mkdir();
}
if (sourceFile.exists()) {
String fileName = sourceFile.getName().replace(".png", "");
BufferedImage bufferedImage = ImageIO.read(sourceFile);
int type = bufferedImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : bufferedImage.getType();
BufferedImage resizedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, type);
Graphics2D graphics2d = resizedImage.createGraphics();
graphics2d.drawImage(bufferedImage, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);//resize goes here
graphics2d.dispose();
ImageIO.write(resizedImage, "png", new File( destinationDir + fileName +".png" ));
int oldImageWidth = bufferedImage.getWidth();
int oldImageHeight = bufferedImage.getHeight();
System.out.println(sourceFile.getName() +" OldFile with Dimensions: "+ oldImageWidth +"x"+ oldImageHeight);
System.out.println(sourceFile.getName() +" ConvertedFile converted with Dimensions: "+ IMAGE_WIDTH +"x"+ IMAGE_HEIGHT);
//Print the image file
PrintActionListener printActionListener = new PrintActionListener(resizedImage);
printActionListener.run();
} else {
System.err.println(destinationFile.getName() +" File not exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Reference of PrintActionListener.java
package com.test.convert;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintActionListener implements Runnable {
private BufferedImage image;
public PrintActionListener(BufferedImage image) {
this.image = image;
}
#Override
public void run() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(new ImagePrintable(printJob, image));
if (printJob.printDialog()) {
try {
printJob.print();
} catch (PrinterException prt) {
prt.printStackTrace();
}
}
}
public class ImagePrintable implements Printable {
private double x, y, width;
private int orientation;
private BufferedImage image;
public ImagePrintable(PrinterJob printJob, BufferedImage image) {
PageFormat pageFormat = printJob.defaultPage();
this.x = pageFormat.getImageableX();
this.y = pageFormat.getImageableY();
this.width = pageFormat.getImageableWidth();
this.orientation = pageFormat.getOrientation();
this.image = image;
}
#Override
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex == 0) {
int pWidth = 0;
int pHeight = 0;
if (orientation == PageFormat.PORTRAIT) {
pWidth = (int) Math.min(width, (double) image.getWidth());
pHeight = pWidth * image.getHeight() / image.getWidth();
} else {
pHeight = (int) Math.min(width, (double) image.getHeight());
pWidth = pHeight * image.getWidth() / image.getHeight();
}
g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null);
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
}
}
Output:
04-Request-Headers_1.png OldFile with Dimensions: 1224x1584
04-Request-Headers_1.png ConvertedFile converted with Dimensions: 800x1000
After conversion of a image a Print window will be open for printing the converted image. The window displays like below, Select the printer from Name dropdown and Click OK button.
You can use either of the following to improve the quality of the scaling. I believe BiCubic gives better results but is slower than BILINEAR.
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
I would also not use Image.getScaledInstance() as it is very slow. I'm not sure about the printing as I'm struggling with similar issues.

Categories