Java / JAI - save an image gray-scaled - java

I try to save the tiff instead of coloure gray-scaled. How could I do this? (JAI must be used, because it is a tiff!)
Thanks a lot in advance & Best Regards.

What you want is to download the JAI Image I/O Tools, which provides ImageIO adapters for JAI. Once you've installed that, it's smooth sailing.
final BufferedImage in = ImageIO.read(new File("frabozzle.tif"));
final BufferedImage out = new BufferedImage(
in.getWidth(), in.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
out.getGraphics().drawImage(in, 0, 0, null);
ImageIO.write(out, "TIFF", new File("graybozzle.tif"));

Given a BufferedImage, you can use the filter() method of ColorConvertOp, as shown in this example.

Related

Java Image Colorprofile Issue

I resize my images via imageio but images which use the Color profile
sRGB IEC61966-2.1- IEC 61966-2.1 Default RGB colour space turn out green-ish:
Source Image:
Output Image:
The strange thing is that it seems that imageio is somehow breaking during runtime. A restart of my JVM somehow cures the issue and it does not happen for a while. Other images which don't use this special Colorprofile resize just fine.
I'm running JDK 1.8.0_25 and use the following image jars:
jai_imageio-1.1.jar
jmage-0.7-2.jar
image4j-0.7.jar
jai_codec-1.1.3.jar
jai_core-1.1.3.jar
Has someone maybe encountered this issue before?
Here's the sample code that works for me. I'm using a slightly more updated Java version.
$ java -version
java version "1.8.0_121"
Source code:
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File(args[0]));
BufferedImageOp op = new AffineTransformOp(AffineTransform.getScaleInstance(1.43, 1.43), AffineTransformOp.TYPE_BICUBIC);
Rectangle bounds = op.getBounds2D(image).getBounds();
// NOTE: Passing null as last param to filter() works fine, but creates a destination
// image (TYPE_INT_ARGB) that JPEGImageWriter will write in a way other software
// interprets as CMYK rather than ARGB. It does not look like your result image, though...
// And, it works fine if you write the image as PNG, even if passing null.
BufferedImage result = op.filter(image, new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_3BYTE_BGR));
ImageIO.write(result, "PNG", new File("foo.png"));
ImageIO.write(result, "JPEG", new File("foo.jpg"));
}
The resulting foo.jpg looks like this:

Is there an efficient way to crop out PDF and save as image (.JPG) via java program?

I am Currently using ICEPDF to render PDF files and display it in my java Swing application (in Internal Frame). Now I want to add crop features to my Java application. Like, if I click a button, I can drag required portion of PDF and save it as Image in my local storage.
Is there an efficient way to crop out PDF and save as image (.JPG) via java program?
Ghost4J library (http://ghost4j.sourceforge.net), is your best option:
3 simple step:
Load PDF files:
PDFDocument document = new PDFDocument();
document.load(new File("test.pdf"));
Create the renderer:
SimpleRenderer renderer = new SimpleRenderer();
// set resolution (in DPI)
renderer.setResolution(600);
Render:
List<Image> images = renderer.render(document);
Then you can do what you want with your image objects, for example, you can write them as JPG like this:
for (int i = 0; i < images.size(); i++) {
ImageIO.write((RenderedImage) images.get(i), "jpg", new File((i + 1) + ".jpg"));
}
Ghost4J uses the native Ghostscript API so you need to have a Ghostscript installed.
EDIT: investigating a bit, if you convert the PDF to Image you won't have much problem to crop them:
BufferedImage is a(n) Image, so the implicit cast that you're doing in the second line is able to be compiled directly. If you knew an Image was really a BufferedImage, you would have to cast it explicitly like so:
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
Then you can crop it with BufferedImage::getSubimage method:
private BufferedImage cropImage(BufferedImage src, Rectangle rect) {
BufferedImage dest = src.getSubimage(rect.x, rect.y, rect.width, rect.height);
return dest;
}

Saving a dynamically created Image to a File.

I have an image, I read the image, add a few things to image (like some text etc).
All this I do inside a JPanel.
Now, I want to save the resulting image to a .png file.
I think, there is a way to do this for a buffered image using ImageIO.write()
But I cannot convert the dynamically created image to a BufferedImage.
Is there a way I can go about this ?
You can use the Screen Image class.
It will create a BufferedImage of your JPanel. The class also has code to write the image to a file.
All this I do inside a JPanel.
Do it instead in another BufferedImage displayed in a JLabel. The code can get a Graphics2D object using BufferedImage.createGraphics() method. Paint the image and text to the new Graphics2D instance and you then the new image can be saved directly, along with changes.
Use Following method it worked for me...
void TakeSnapShot(JPanel panel,String Locatefile){
BufferedImage bi = new BufferedImage(panel.getSize().width, panel.getSize().height,BufferedImage.TYPE_INT_RGB);
panel.paint(bi.createGraphics());
File image = new File(Locatefile);
try{
image.createNewFile();
ImageIO.write(bi, "png", image);
}catch(Exception ex){
}
}

Stream the content of a scanned image to a file in Java

I'm trying to scan an image and save it to a file given a specific format (Tiff or Jpeg) with a Swing application, using Morena and Sane.
I load the whole image in memory with this process:
SaneSource source = /* source implemented here */;
MorenaImage morenaImage = new MorenaImage(source);
Image image=Toolkit.getDefaultToolkit().createImage(morenaImage);
BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
ImageIO.write(bimg, "jpg", new File(filename));
I'm pretty sure there is a better way to do this without eating all my memory, like streaming the content of my scanned image in cache to the file with a Consumer / Observer, but I couldn't wrap my mind good enough around these notions to create my own solution.
Could you please help me down the path to better image processing?
Thanks in advance, david
You should attach an ImageConsumer (that will write image to OutputStream using your favorite image format) directly to an ImageProducer (SaneSource or MorenaImage if you wish). You can find ImageConsumer example that encodes image as PPM and transfers it to OutputStream here. You'll need to write something like this to use this example:
ImageProducer prod = ... your producer here ....;
PpmEncoder ppm = new PpmEncoder(prod, myOutputStream);
ppm.encode();

How can I get a less pixelated screenshot in java?

I am using a BufferedImage in java after capturing the image out of a JFrame. I need some way to sharpen the image such that when it's enlarged it doesn't look so pixelated. Here's the thing it has to keep the same image size.
Here's the code I'm using to capture the image.
private void grabScreenShot() throws Exception
{
BufferedImage image = (BufferedImage)createImage(getSize().width, getSize().height);
paint(image.getGraphics());
try{
//ImageIO.write(image, "jpg", new File(TimeTable.path+"\\TimeLine.jpg"));
ImageIO.write(image, "jpg", new File("C:\\Users\\"+TimeTable.user+"\\AppData\\TimeLineMacroProgram\\TimeLine.jpg"));
//ImageIO.write(image, "jpg", new File("C:\\ProgramData\\TimeLineMacroProgram\\TimeLine.jpg"));
System.out.println("Image was created");
}
catch (IOException e){
System.out.println("Had trouble writing the image.");
throw e;
}
}
And here's the image it creates
JPG is ill suited for screenshots. It's designed for complex and colorful pictures wherein information loss during compression is nearly negligible, such as photos. For screenshots you should rather be using GIF or, better, PNG.
ImageIO.write(image, "png", new File("C:\\Users\\"+TimeTable.user+"\\AppData\\TimeLineMacroProgram\\TimeLine.png"));
You only end up with a bigger file, but you get pixelperfect sharpness and detail back, which is simply impossible with JPG.
I think it's because you're writing it out as a JPEG file.
I'd change the format to something non-lossy, or else force the writer to not use compression by accessing and changing its ImageWriteParam.

Categories

Resources