Incorrect number of Contours on javacv? - java

I have write simple code to retrieve number of Contours in a image and get the number of Contours in the image. But it always gives incorrect answer. Please can some one explain about this ?
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import java.io.File;
import javax.swing.JFileChooser;
public class TestBeam {
public static void main(String[] args) {
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage grayImage = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, 1);
cvCvtColor(src, grayImage, CV_RGB2GRAY);
cvThreshold(grayImage, grayImage, 127, 255, CV_THRESH_BINARY);
CvSeq cvSeq=new CvSeq();
CvMemStorage memory=CvMemStorage.create();
cvFindContours(grayImage, memory, cvSeq, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
System.out.println(cvSeq.elem_size());
CanvasFrame cnvs=new CanvasFrame("Beam");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
cnvs.showImage(src);
//cvShowImage("Final ", src);
}
}
This is the sample image that I used
But Code always returns output as 8. Please can someone explain this?

cvSeq.elem_size() will return size of sequence element in bytes and not the number of contours. That is why output is 8 every time.
Please refer following link for more information.
http://opencv.willowgarage.com/documentation/dynamic_structures.html#cvseq
For finding number of contours you can use following snippet
int i = 0;
while(cvSeq != null){
i = i + 1;
cvSeq = cvSeq.h_next();
}
System.out.println(i);
With the parameters you have provided CV_RETR_EXTERNAL will only provide external contour that is image boundary in your image(provided you are not inverting the image). You can use CV_RETR_LIST to get all the contours. Visit following link for more information on the parameters.
http://opencv.willowgarage.com/documentation/structural_analysis_and_shape_descriptors.html#findcontours

Related

OpenCV library in Tomcat(8.5.32) Server unable to execute

Facing issue while image processing code setup. In spite of doing all code changes and different approaches facing the issue.
Libraries used – OpenCV – 3.4.2
Jars Used – opencv-3.4.2-0
tess4j-3.4.8
Lines added in pom.xml
<!-- https://mvnrepository.com/artifact/org.openpnp/opencv -->
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.4.2-0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.4.8</version>
</dependency>
Steps for OpenCV installation :
Download opencv.exe from the official site’
Run opencv.exe, it will create an opencv folder
We have now the opencv library available which we can use for eclipse.
Steps for Tesseract installation :
Download tess4j.zip file from the official link
Extract the zip folder after download
Provide the path of the tess4j folder
Following are the steps which we have performed for the setup in eclipse :
We have added native library by providing path to openCV library from build path settings
We downloaded tesseract for image reading.
We provided the path to the Tesseraact in the code
We have used System.loadlibrary(Core.NATIVE_LIBRARY_NAME) and openCv.loadLocally() for loading the library.
Then we have made the WAR export for deployment
There has been no changes or setup in apache tomcat
For loading the libraries in Tomcat we have to provide some setup here :-
Now for the new code we have used, Load Library static class in the code (as solutions stated on stack overflow)
In here System.loadLibrary is not working
We had to use System.load along with hardcoded path which is resulting in internal error
We have used System.load – 2 time in the static class out of which the when the 1st one is giving -std error -bad allocation
As there are 2 paths in opencv-
This is the 1st one
System.load("C:\Users\Downloads\opencv\build\bin\opencv_java342.dll");
and the 2nd one is giving the assertion error based on which one is kept above
This is the 2nd one
System.load("C:\User\Downloads\opencv\build\java\x64\opencv_java3412.dll");
The code is executing till mid-way and then getting out and till now not yet code has reached till tesseract.
Here is the code for the same :
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.opencv.core.Core;
import org.opencv.core.CvException;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import net.sourceforge.tess4j.Tesseract;
import nu.pattern.OpenCV;
public class ReadImageBox {
public String readDataFromImage(String imageToReadPath,String tesseractPath)
{
String result = "";
try {
String i = Core.NATIVE_LIBRARY_NAME;
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
logger.info("Img to read "+imageToReadPath);
String imagePath =imageToReadPath; // bufferNameOfImagePath = "";
logger.info(imagePath);
/*
* The class Mat represents an n-dimensional dense numerical single-channel or
* multi-channel array. It can be used to store real or complex-valued vectors
* and matrices, grayscale or color images, voxel volumes, vector fields, point
* clouds, tensors, histograms (though, very high-dimensional histograms may be
* better stored in a SparseMat ).
*/
logger.info("imagepath::"+imagePath);
OpenCV.loadLocally();
logger.info("imagepath::"+imagePath);
//logger.info("Library Information"+Core.getBuildInformation());
logger.info("imagepath::"+imagePath);
Mat source = Imgcodecs.imread(imagePath);
logger.info("Source image "+source);
String directoryPath = imagePath.substring(0,imagePath.lastIndexOf('/'));
logger.info("Going for Image Processing :" + directoryPath);
// calling image processing here to process the data from it
result = updateImage(100,20,10,3,3,2,source, directoryPath,tesseractPath);
logger.info("Data read "+result);
return result;
}
catch (UnsatisfiedLinkError error) {
// Output expected UnsatisfiedLinkErrors.
logger.error(error);
}
catch (Exception exception)
{
logger.error(exception);
}
return result;
}
public static String updateImage(int boxSize, int horizontalRemoval, int verticalRemoval, int gaussianBlur,
int denoisingClosing, int denoisingOpening, Mat source, String tempDirectoryPath,String tesseractPath) throws Exception{
// Tesseract Object
logger.info("Tesseract Path :"+tesseractPath);
Tesseract tesseract = new Tesseract();
tesseract.setDatapath(tesseractPath);
// Creating the empty destination matrix for further processing
Mat grayScaleImage = new Mat();``
Mat gaussianBlurImage = new Mat();
Mat thresholdImage = new Mat();
Mat morph = new Mat();
Mat morphAfterOpreation = new Mat();
Mat dilate = new Mat();
Mat hierarchy = new Mat();
logger.info("Image type"+source.type());
// Converting the image to gray scale and saving it in the grayScaleImage matrix
Imgproc.cvtColor(source, grayScaleImage, Imgproc.COLOR_RGB2GRAY);
//Imgproc.cvtColor(source, grayScaleImage, 0);
// Applying Gaussain Blur
logger.info("source image "+source);
Imgproc.GaussianBlur(grayScaleImage, gaussianBlurImage, new org.opencv.core.Size(gaussianBlur, gaussianBlur),
0);
// OTSU threshold
Imgproc.threshold(gaussianBlurImage, thresholdImage, 0, 255, Imgproc.THRESH_OTSU | Imgproc.THRESH_BINARY_INV);
logger.info("Threshold image "+gaussianBlur);
// remove the lines of any table inside the invoice
Mat horizontal = thresholdImage.clone();
Mat vertical = thresholdImage.clone();
int horizontal_size = horizontal.cols() / 30;
if(horizontal_size%2==0)
horizontal_size+=1;
// showWaitDestroy("Horizontal Lines Detected", horizontal);
Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
new org.opencv.core.Size(horizontal_size, 1));
Imgproc.erode(horizontal, horizontal, horizontalStructure);
Imgproc.dilate(horizontal, horizontal, horizontalStructure);
int vertical_size = vertical.rows() / 30;
if(vertical_size%2==0)
vertical_size+=1;
// Create structure element for extracting vertical lines through morphology
// operations
Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
new org.opencv.core.Size(1, vertical_size));
// Apply morphology operations
Imgproc.erode(vertical, vertical, verticalStructure);
Imgproc.dilate(vertical, vertical, verticalStructure);
Core.absdiff(thresholdImage, horizontal, thresholdImage);
Core.absdiff(thresholdImage, vertical, thresholdImage);
logger.info("Vertical Structure "+verticalStructure);
Mat newImageFortest = thresholdImage;
logger.info("Threshold image "+thresholdImage);
// applying Closing operation
Imgproc.morphologyEx(thresholdImage, morph, Imgproc.MORPH_CLOSE, Imgproc.getStructuringElement(
Imgproc.MORPH_RECT, new Size(denoisingClosing, denoisingClosing)));
logger.info("Morph image "+morph);
// applying Opening operation
Imgproc.morphologyEx(morph, morphAfterOpreation, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(
Imgproc.MORPH_RECT, new Size(denoisingOpening, denoisingOpening)));
logger.info("Morph After operation image "+morphAfterOpreation);
// Applying dilation on the threshold image to create bounding box edges
Imgproc.dilate(morphAfterOpreation, dilate,
Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(boxSize, boxSize)));
logger.info("Dilate image "+dilate);
// creating string buffer object
String text = "";
try
{
// finding contours
List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); // A list to store all the contours
// finding contours
Imgproc.findContours(dilate, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
logger.info("Contour List "+contourList);
// Creating a copy of the image
//Mat copyOfImage = source;
Mat copyOfImage = newImageFortest;
logger.info("Copy of Image "+copyOfImage);
// Rectangle for cropping
Rect rectCrop = new Rect();
logger.info("Rectangle Crop New Object "+rectCrop);
// loop through the identified contours and crop them from the image to feed
// into Tesseract-OCR
for (int i = 0; i < contourList.size(); i++) {
// getting bound rectangle
rectCrop = Imgproc.boundingRect(contourList.get(i));
logger.info("Rectangle cropped"+rectCrop);
// cropping Image
Mat croppedImage = copyOfImage.submat(rectCrop.y, rectCrop.y + rectCrop.height, rectCrop.x,
rectCrop.x + rectCrop.width);
// writing cropped image to disk
logger.info("Path to write cropped image "+ tempDirectoryPath);
String writePath = tempDirectoryPath + "/croppedImg.png";
logger.info("writepath"+writePath);
// imagepath = imagepath.
Imgcodecs.imwrite(writePath, croppedImage);
try {
// extracting text from cropped image, goes to the image, extracts text and adds
// them to stringBuffer
logger.info("Exact Path where Image was written with Name "+ writePath);
String textExtracted = (tesseract
.doOCR(new File(writePath)));
//Adding Seperator
textExtracted = textExtracted + "_SEPERATOR_";
logger.info("Text Extracted "+textExtracted);
textExtracted = textExtracted + "\n";
text = textExtracted + text;
logger.info("Text Extracted Completely"+text);
// System.out.println("Andar Ka Text => " + text.toString());
} catch (Exception exception) {
logger.error(exception);
}
writePath = "";
logger.info("Making write Path empty for next Image "+ writePath);
}
}
catch(CvException ae)
{
logger.error("cv",ae);
}
catch(UnsatisfiedLinkError ae)
{
logger.error("unsatdif",ae);
}
catch(Exception ae)
{
logger.error("general",ae);
}
// converting into string
return text.toUpperCase();
}
// convert Mat to Image for GUI output
public static Image toBufferedImage(Mat m) {
// getting BYTE_GRAY formed image
int type = BufferedImage.TYPE_BYTE_GRAY;
if (m.channels() > 1) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels() * m.cols() * m.rows();
byte[] b = new byte[bufferSize];
m.get(0, 0, b); // get all the pixels
// creating buffered Image
BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
// returning Image
return image;
}
// method to display Mat format images using the GUI
private static void showWaitDestroy(String winname, Mat img) {
HighGui.imshow(winname, img);
HighGui.moveWindow(winname, 500, 0);
HighGui.waitKey(0);
HighGui.destroyWindow(winname);
}
}

Want to store detected cropped images in a file

I have successfully detected human faces using haarcascade in opencv. I have run a loop in which it will crop only the detected portion of the image and store into the system. But it crops only one detected face and rest of the faces doesn't get cropped and save into the system. I am attaching my code. please help to to solve my issue. Eg. if image contains 4 people then haarcascade detect 4 faces but the loop only crops one detected image and rest 3 images does not get cropped..!
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetection
{
public static void main(String[] args)
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector = new CascadeClassifier();
faceDetector.load("G:\\haarcascade_frontalface_alt.xml");
//System.out.println ( "Working" );
// Input image
Mat image = Imgcodecs.imread("G:\\sample.jpg");
// Detecting faces
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces",
faceDetections.toArray().length));
// Creating a rectangular box showing faces detected
Rect rectCrop=null;
for (Rect rect : faceDetections.toArray())
{
Imgproc.rectangle(image, new Point(rect.x, rect.y),
new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0),2);
rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
}
Rect[] count = faceDetections.toArray();
System.out.println(""+count.length);
// Saving the output image
String filename = "Ouput.jpg";
Imgcodecs.imwrite("G:\\"+filename, image);
Mat markedImage = new Mat(image,rectCrop);
Imgcodecs.imwrite("G:\\crop1.jpg",markedImage );
}
}
Your code is looping over all of them and choosing the last face and then saving that face. You need it to either a) crop them separately and add meta that tags them together, or b)crop them all together.
For option a, inside your loop, you need to save each file separately. Simply move you saving file code into your loop and adjust it so that it works.
For option b, you might need some additional software to crop all of the faces together. This could be done a number of ways. You could concatenate all of the a)pixle arrays, or b)individual bytes. Then when you need to find connections between people you simply have to run the face recognition software on the files again.

Determine whether an image is grayscale in Java

I'm using something along the lines of this to do a (naive, apparently) check for the color space of JPEG images:
import java.io.*;
import java.awt.color.*;
import java.awt.image.*;
import javax.imageio.*;
class Test
{
public static void main(String[] args) throws java.lang.Exception
{
File f = new File(args[0]);
if (f.exists())
{
BufferedImage bi = ImageIO.read(f);
ColorSpace cs = bi.getColorModel().getColorSpace();
boolean isGrayscale = cs.getType() == ColorSpace.TYPE_GRAY;
System.out.println(isGrayscale);
}
}
}
Unfortunately this reports false for images that (visually) appear gray-only.
What check would do the right thing?
You can use this code:
File input = new File("inputImage.jpg");
BufferedImage image = ImageIO.read(input);
Raster ras = image.getRaster();
int elem = ras.getNumDataElements();
System.out.println("Number of Elems: " + elem);
If the number of elems returns 1, then its a greyscale image. If it returns 3, then its a color image.
the image looks like gray beacuse the r=g=b but actually it is a full color image, it has three channel r g b and the real gray image only have one channel

How to identify and fill I shape Contours in javacv?

I'm developing project on javacv and I need to know how to identify following image and fill that Image using particular color ?
I try to go through this question and This is the image that I use
I try to go through this code and I developed a code in javacv
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import javax.swing.JFileChooser;
public class PolyGonIdentification {
public static void main(String[] args) {
CanvasFrame cnvs=new CanvasFrame("Polygon");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvCvtColor(src, gry, CV_BGR2GRAY);
cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV);
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
System.out.println(squares.total());
for (int i=0; i<squares.total(); i++)
{
cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
}
IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null);
cvDilate(gry, gry, mat, CV_C);
cvErode(gry, gry, mat, CV_C);
cnvs.showImage(gry);
}
}
My final result should be like this image
Put above code resulted this kind of image. Please can some one help me to resolve this issue ?
You can archive that using this code
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import javax.swing.JFileChooser;
public class Ishape {
public static void main(String[] args) {
CanvasFrame cnvs=new CanvasFrame("Polygon");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvCvtColor(src, gry, CV_BGR2GRAY);
cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV);
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
CvSeq ss=null;
for (int i=0; i<1; i++)
{
cvDrawContours(gry, squares, CvScalar.WHITE, CV_RGB(248, 18, 18), 1, -1, 8);
ss=cvApproxPoly(squares, sizeof(CvContour.class), storage, CV_POLY_APPROX_DP, 8, 0);
}
IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null);
cvDilate(gry, gry, mat, CV_C);
cvErode(gry, gry, mat, CV_C);
cnvs.showImage(gry);
}
}
Result
This will be resulted out put and I believe this might help you to solve your problem.
You just have to find contours using external retrieval mode:
CV_RETR_EXTERNAL retrives only the extreme outer contours
In your situation this is the best mode because you have one external contour and this contour is what you're looking for. Here's documantation.
After this just draw it using drawContours with CV_FILLED as thickness parameter to fill external polygon.
The code does exactly what is supposed to do:
// as stated [in the answer to your previous question][1], findContours leaves gry set to 0, or black
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
System.out.println(squares.total()); // nothing interesting
for (int i = 0; i < squares.total(); i++)
{
// draw a gray contour (127) on a black image
cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
}
To correct it, you have to set gry to 255 (white, before any call to drawContours, and draw black contours! that is 0, not 127.)

RGBImageFilter Produces All Black Empty Images

I'm trying to use the RGBImageFilter to produce a simple transparency effect on an image, however during testing I found that all of my filtered images come out as blank with black backgound. I simplified my filter to a simple "No Operation" filter that just returns the RGB value that was passed in. Even this simplified NoOp filter produces a blank image. I've tried this with importing and exporting both JPG and PNG with the same affect. I've tried various example filters off the internet that produce the same all black image problem. Has anyone encountered this before? The "load status" and the "write status" below are both returning true so I know from MediaTracker that the original image is loading fully and that the produced image is writing successfully. Thanks in advance for any help!
Here is the code:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
class NoOpFilter extends RGBImageFilter {
public NoOpFilter() {
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
return rgb;
}
public static void main(String[] args) throws Exception{
String originalImageLocation = args[0];
String baseFileName = args[1];
String directory = args[2];
ImageIcon originalImage = new ImageIcon(Toolkit.getDefaultToolkit().getImage(originalImageLocation));
NoOpFilter filter2 = new NoOpFilter();
ImageProducer ip = new FilteredImageSource(originalImage.getImage().getSource(), filter2);
Image filteredImage = Toolkit.getDefaultToolkit().createImage(ip);
System.out.println("Load Status: " + ( originalImage.getImageLoadStatus() == java.awt.MediaTracker.COMPLETE));
BufferedImage bim = new BufferedImage( originalImage.getIconWidth(),originalImage.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bim.createGraphics();
g2.drawImage(filteredImage, 0, 0, null);
g2.dispose();
File file = new File(directory + File.separator + baseFileName + ".png");
file.createNewFile();
boolean status = ImageIO.write(bim, "png", file);
System.out.print("write() status: " + status);
}
}
Here is the fix that worked for me. Right after creating the fileredImage above, it was necessary to actually add the filtered image to a JLabel and the JLabel to a JFrame in order to force it to be rendered to screen first before writing it into my buffer and saving to disk:
...
Image filteredImage = Toolkit.getDefaultToolkit().createImage(ip);
ImageIcon swingImage = new ImageIcon(filteredImage);
JFrame frame = new JFrame();
JLabel label = new JLabel(swingImage);
frame.add(label);
frame.setVisible(true);
frame.dispose();
BufferedImage bim = new BufferedImage( originalImage.getIconWidth(),originalImage.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bim.createGraphics();
g2.drawImage(filteredImage, 0, 0, null);
g2.dispose();
File file = new File(directory + File.separator + baseFileName + ".png");
file.createNewFile();
boolean status = ImageIO.write(bim, "png", file);
System.out.print("write() status: " + status);
}
I don't quite yet understand why this can't be done completely off-screen like I wanted to do, however it was easy enough to dispose the JFrame right after opening it. A better solution would not require using UI components to do this since I don't really need anything displayed on screen in this case and just want the end product.
Thanks to Hovercraft for pointing me in the right direction.
You have to call prepareImage, then you don't have to use the JLabel-workaround.
Image filteredImage = Toolkit.getDefaultToolkit().createImage(ip);
Toolkit.getDefaultToolkit().prepareImage(filteredImage, -1, -1, null);

Categories

Resources