Resize existing JavaFX image - javafx

I have to load images:
Image image = new Image(f.toURI().toString(), width, height, true, true);
Since I also have tiffs, I have to load them differently using JAI:
BufferedImage read = ImageIO.read(f);
Image image = SwingFXUtils.toFXImage(read, null);
Now I have an Image object, but it has the wrong size. Image offers no methods to resize the object. How do I resize is so it has the same size as if I would load a jpg or png with the shown line?

Seems like there should be an easier way to do this, but you can try:
BufferedImage read = ImageIO.read(f);
int[] pixels = new int[width * height] ;
PixelGrabber grabber = new PixelGrabber(read.getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, width, height, 0, pixels, width);
grabber.grabPixels();
WritableImage fxImage = new WritableImage(width, height);
PixelWriter pw = fxImage.getPixelWriter();
pw.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), pixels, 0, width);
Now fxImage is a javafx.scene.image.Image that has dimensions width by height.
Be aware that PixelGrabber.grabPixels() is a blocking call, so you will need to handle InterruptedExceptions. For large images you might want to do this in a Task executed on a background thread, so as not to block the FX Application Thread.

Related

Enlarge Image Canvas

Is there a quick way using System.Drawing to quickly enlarge the image canvas of an .png image? (see example below). The caveat is the background might be transparent and I want to keep it transparent.
Edit: Needs to be in ASP .Net CORE
Alternatively, is there a way of putting the image on a white background that is slightly larger than the image?
After a few days of trial and error, I think I found something that works
Image overlayImage = //get your image here from file or url.
xloc = //x coord where to start overlay image.
yloc = //y coord where to start overlay image.
canvasWidth = //width of background canvas
canvasHeight = //height of background canvas
Bitmap baseImage = new Bitmap(canvasWidth, canvasHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics graphics = Graphics.FromImage(baseImage))
{
using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White))
{
graphics.FillRectangle(myBrush, new Rectangle(0, 0, canvasWidth, canvasHeight)); // white rectangle
}
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.DrawImage(overlayImage, xloc, yloc);
} // graphics will be disposed at this line

JavaFx ImageView

How to clear the contents of the ImageView object so that I can use the PixelWriter object to redraw an image?
Thanks
e.g.
ImageView tmpView = new ImageView();
WritableImage writableImage = new WritableImage(width, height);
PixelWriter pixelWriter = writableImage.getPixelWriter();
...
tmpView.setImage(writableImage);
...
//draw the first image
...
//clear the contents of tmpView
//???
//redraw imgView
pixelWriter.setColor(x, y, color);
In the argb color model, 0 represents transparent. Since a new int[n] creates an int array of length n with all values initialized to 0, the following will make all the pixels transparent:
pixelWriter.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(),
new int[width*height], 0, width);
If you want all the pixels to be white (for example), then you can fill the array with the appropriate value:
int[] white = new int[width*height];
Arrays.fill(white, 0xffffffff);
pixelWriter.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(),
white, 0, width);
Another option is just to create a new WritableImage and set it in the image view.
Without knowing any further details of the original task I'd use a Canvas here instead of an Image/ImageView. With the drawing capabilities of the Canvas you can easily clear the area without having to create possibly huge arrays which have to be filled with color values.

Why is this code shrinking my image?

I've take some code and reduced it down to a few lines which still reproduce the error i am having. In this case, I am taking an image that is 448x298 in size and trying to overlay it on top of a white background that's 600x450.
So I'm expecting to get an image that's 600x450 with a white background and my original image laid on top of it starting in the upper right corner. And i expect my original image to remain it's original size. Instead the original image is going from 448x298 to approximately (give or take a pixel or two) 143x95
Here is the reduced code that's doing this:
System.Drawing.Image oImage = new Bitmap(600, 450);
Graphics oGraphic = Graphics.FromImage(oImage);
oGraphic.FillRectangle(Brushes.White, 0, 0, 600, 450);
oGraphic.DrawImage(image, new Point(0,0));
return (Bitmap)oImage;
You have to specify the target size. The overload you chose scales the image from the source dpi to the target dpi. As explained in another question, you should do this:
System.Drawing.Image oImage = new Bitmap(600, 450);
Graphics oGraphic = Graphics.FromImage(oImage);
oGraphic.FillRectangle(Brushes.White, 0, 0, 600, 450);
oGraphic.DrawImage(image, 0,0, image.Width, image.Height);
return (Bitmap)oImage;

ImageView not resizing image

I want to use an ImageView to resize an image, however the image is not being resized:
ImageView imageView = new ImageView(image);
imageView.setPreserveRatio(true);
imageView.setFitHeight(40);
System.out.println("imageview image width = " + imageView.getImage().getWidth());
System.out.println("imageview image height = " + imageView.getImage().getHeight());
The output is
imageview image width = 674.0
imageview image height = 888.0
However, the width should be 40. My ImageView is not attached to any scene and I also don't want to attach it, it shall only be used for image resizing. Is there any way to force the ImageView to resize its image, even though the ImageView is not attached to any scene? The reason I am using an ImageView for resizing is, that I want to resize an Image in RAM, without reading it again from the disk, please see this question for more details.
Thanks for any hint!
Using an ImageView for resizing seems to be very hacky.
A better approach is to convert your Image into a BufferedImage and do the resizing the old way. (JavaFx does not (yet) provide an internal way to resize memory images)
int width = 500; // desired size
int height = 400;
Image original = ...; // fx image
BufferedImage img = new BufferedImage(
(int)original.getWidth(),
(int)original.getHeight(),
BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(original, img);
BufferedImage rescaled = Scalr.rescaleImage(img, width, heigth); // the actual rescale
// convert back to FX image
WritableImage rescaledFX = new WritableImage(width, heigth);
SwingFXUtils.toFXImage(rescaled, rescaledFX);
Where as Scalr is a nice library for resizing images in native java. Obviously, you can use other/simpler methods of rescaling, but the image quality won't be that nice.

How can an image be cropped based on a set scale of random image?

Working on allowing the upload of images which can range in a variety of size, then allowing to crop a predefined area of the image for a thumbnail.
The thumbnail size is predefined to 150x150. Using the Jcrop.js tool to select a section of the image.
Problem:
When displaying the uploaded image in a smaller size than the original image by implementing set height/width on the image rendered, then there is a scale factor that comes into play when selecting an area to crop.
You either have to scale down the cropping area proportionately or you have to scale the image in relation to the actual image's size in comparison to its displayed size.
Question:
How do I figure out the scale of the browser displayed image vs. original image? I am currently using the following code to save the image, but how would I take into consideration the scaling?
public static Image CropImage(Image originalImage, int x, int y, int width, int height)
{
var bmp = new Bitmap(width, height);
bmp.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
using (var graphic = Graphics.FromImage(bmp))
{
graphic.SmoothingMode = SmoothingMode.AntiAlias;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.PixelOffsetMode = PixelOffsetMode.HighSpeed;
graphic.DrawImage(originalImage, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
return bmp;
}
}
Bonus Question:
Another problem I discovered, is that there seems to be no efficient way to transfer the original file's ImageFormat when creating a new Bitmap which creates a ImageFormatMemoryBMP and when you attempt to call Bitmap.Save(memorystream, original rawformat) it will blow up. And bitmap RawFormat has no setter.
So how can you set the format on a new bitmap?
I think perhaps that this problem is solved purely on the front end, no need to use any server side for this.
Jcrop has a built in scale factor handler.
http://deepliquid.com/content/Jcrop_Sizing_Issues.html
Now you can use this in two ways, as I understand it. Either to 'resize' the image for you on the front end using 'box sizing', or you can tell it the 'truesize' of the image and it will work out the scale factor and handle the coordinates for you on it's own.
Box sizing
$('#cropbox').Jcrop({ boxWidth: 450, boxHeight: 400 });
True Size
$.Jcrop('#cropbox',{ trueSize: [500,370] });
Using the true size method you will need to invoke jcrop using the api method:
http://deepliquid.com/content/Jcrop_API.html#API_Invocation_Method
var jcrop_api,
options = { trueSize: [500,370] };
$('#target').Jcrop(options,function(){
jcrop_api = this;
});
Good luck!

Resources