Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Image processing 1

Status
Not open for further replies.

eramgarden

Programmer
Aug 27, 2003
279
US
I have inherited a Java application, it creates JPG files but the images are blury...

1.The input is a "tif" file and it converts it to a jpg file.

2. This is the class it uses:

3. It reads the file with this line:
imageSRC = JAIImageReader2.readImage(fileName);
renImage=imageSRC

4. Then down in the code, it uses these 2 lines to convert the image:

RenderedImageAdapter ria = new RenderedImageAdapter(renImage);
BufferedImage bi = ria.getAsBufferedImage()

5. The image is not sharp...
Any ideas on what to do would be great..
 
Ahhh, another JAI meddler. Its a tricky API, I'll grant you !!!

try this code :

Code:
// load it
RenderedOp image = JAI.create("fileload", "C:\mytiff.tif");

// maybe do some stuff with the image
// ...
// PS you can get a BufferedImage like this :
// BufferedImage buff = image.getAsBufferedImage()

// save it as jpeg
// This will encode a BufferedImage or a RenderedOp object
// - here it is encoding the RenderedOp (not the BufferedImage)
String outputfilename = "C:\mytiff.jpg";

System.out.println("Writing image to :: " +outputfilename);
FileOutputStream out = null;
try {
	out = new FileOutputStream(outputfilename);
} catch(IOException ioe) {
	System.out.println("IOException - " +ioe);
}
JPEGEncodeParam encodeParam = new JPEGEncodeParam();
encodeParam.setQuality(0.75F);
ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", out,encodeParam);
try {
	encoder.encode(image);
	out.flush();
	out.close();
	out = null;
} catch (IOException e) {
	System.out.println("IOException at encoding..");
}

			image = null;
 
thanks to sedj for supporting all JAI-victims on their way to heaven!

For people that use JRE 1.4+ i've got a copy and past class i just established while looking at sedj's example. works neatly (don't forget to update the paths when testing).

-----------------------------------------------

public class ImageTreater {
public ImageTreater(String imgPath) {
ParameterBlock pb =(new ParameterBlock()).add(imgPath);
RenderedOp image = JAI.create("fileload", pb, null);
BufferedImage buffImg = image.getAsBufferedImage();
String outputfilename = "c:\\test.jpg";
FileOutputStream out = null;
try {
out = new FileOutputStream(outputfilename);
} catch(IOException ioe) {
System.out.println("IOException - " +ioe);
}
JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(buffImg);
encodeParam.setQuality(0.75F, true);
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(out ,encodeParam);

try {
jpegEncoder.encode(buffImg);
out.flush();
out.close();
out = null;
} catch (IOException e) {
System.out.println("IOException at encoding..");
}
image = null;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top