November 10, 200817 yr Newbies Hi there, I'm having a little problem with running a java script with scriptmaster and was hoping for a little help. The script runs fine in a normal editor but when run using Scriptmaster on a Mac allow the image to be viewed but when you try to delete it, it doesn't allow you to, saying 'the file is current being used by an application'. On PC it has the same problem but also won't display the image. The script is; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.metadata.IIOMetadata; import javax.imageio.stream.FileImageInputStream; import org.w3c.dom.Element; import org.w3c.dom.Node; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class JpegDensity { public static void main(String[] args) throws Exception { File infile = new File("/fmimages/49433.jpg"); File outfile = new File("/fmimages/density7.jpg"); ImageReader reader = ImageIO.getImageReadersByFormatName("jpeg").next(); reader.setInput(new FileImageInputStream(infile), true, false); IIOMetadata data = reader.getImageMetadata(0); BufferedImage image = reader.read(0); Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0"); Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0); for (int i = 0; i < jfif.getAttributes().getLength(); i++) { Node attribute = jfif.getAttributes().item(i); System.out.println(attribute.getNodeName() + "=" + attribute.getNodeValue()); } JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(outfile)); JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image); jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH); jpegEncodeParam.setXDensity(300); jpegEncodeParam.setYDensity(300); jpegEncoder.encode(image, jpegEncodeParam); } } Any help would be greatly appreciated Many thanks in advanced Rob
November 11, 200817 yr Hi Rob - when you create a new InputStream, you need to close it after you're done with it. That is why the system thinks that the file is in use.
Create an account or sign in to comment