Sub SaveImage(img As Image, out As OutputStream)
Dim jo As JavaObject = Me
Dim dpi As Float = 96
jo.RunMethod("saveImage", Array(out,img, dpi))
out.Close
End Sub
#if Java
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;
public static void saveImage(OutputStream output, Image image, float dpi) throws IOException {
final String formatName = "png";
for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
ImageWriter writer = iw.next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
continue;
}
setDPI(metadata, dpi);
final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
writer.setOutput(stream);
writer.write(metadata, new IIOImage(SwingFXUtils.fromFXImage(image, null), null, metadata), writeParam);
stream.close();
break;
}
}
private static void setDPI(IIOMetadata metadata, float dpi) throws IIOInvalidTreeException {
// for PMG, it's dots per millimeter
double dotsPerMilli = 1.0 * dpi / 10 / 2.54;
IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
horiz.setAttribute("value", Double.toString(dotsPerMilli));
IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
vert.setAttribute("value", Double.toString(dotsPerMilli));
IIOMetadataNode dim = new IIOMetadataNode("Dimension");
dim.appendChild(horiz);
dim.appendChild(vert);
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
root.appendChild(dim);
metadata.mergeTree("javax_imageio_1.0", root);
}
#End If
CanvasMain1.SetSize(390,472)
Dim MyIm As Image
MyIm = CanvasMain1.Snapshot
Dim Out As OutputStream = File.OpenOutput(DirOut, SUF & "_FOTO_" & edtName.Text &".jpg",False)
Enhance3.SaveImage3(MyIm, Out, "300", "01")
Out.Close
'Static code module
Sub Process_Globals
Private fx As JFX
End Sub
Public Sub SaveImage3(img As Image, out As OutputStream, dpi As String, ppi As String)
Dim jo As JavaObject = Me
jo.RunMethod("saveImage3", Array(out,img, dpi, ppi))
out.Close
End Sub
#if Java
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;
public static void saveImage3(OutputStream output, Image image, String dpi, String ppi) throws IOException {
final String formatName = "jpeg";
for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
ImageWriter writer = iw.next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
continue;
}
setDPI3(metadata, dpi, ppi);
final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
writer.setOutput(stream);
writer.write(metadata, new IIOImage(SwingFXUtils.fromFXImage(image, null), null, metadata), writeParam);
stream.close();
break;
}
}
private static void setDPI3(IIOMetadata metadata, String dpi, String ppi) throws IIOInvalidTreeException {
String metadataFormat = "javax_imageio_jpeg_image_1.0";
IIOMetadataNode root = new IIOMetadataNode(metadataFormat);
IIOMetadataNode jpegVariety = new IIOMetadataNode("JPEGvariety");
IIOMetadataNode markerSequence = new IIOMetadataNode("markerSequence");
IIOMetadataNode app0JFIF = new IIOMetadataNode("app0JFIF");
app0JFIF.setAttribute("majorVersion", "1");
app0JFIF.setAttribute("minorVersion", "2");
app0JFIF.setAttribute("thumbWidth", "0");
app0JFIF.setAttribute("thumbHeight", "0");
app0JFIF.setAttribute("resUnits", ppi);
app0JFIF.setAttribute("Xdensity", dpi);
app0JFIF.setAttribute("Ydensity", dpi);
root.appendChild(jpegVariety);
root.appendChild(markerSequence);
jpegVariety.appendChild(app0JFIF);
metadata.mergeTree(metadataFormat, root);
}
#End If