I work on image slide show application on JavaFX and after a while of working java crash with no free memory error.
In interface code I have:
image = new ImageView();
Group root = new Group(image);
imageScene = new Scene(root, height, width);
primaryStage.setScene(imageScene);
In background thread I set image source for image view:
...
Map<String, Image> imagesMap = new HashMap<>();
...
// Slide thread
if (!imagesMap.containsKey(item.File)) {
Image image = new Image(item.File);
imagesMap.put(item.File, image);
}
Image i = imagesMap.get(item.File);
image.setImage(i);
While app run(with params: -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -Xmx256m) memory allocated to java process always is increasing...
Reduce the image size using the below code
Image img = ic.getImage();
Image newimg = img.getScaledInstance(500, 700, java.awt.Image.SCALE_SMOOTH);
I am sure you are using the original image size in this app.
Related
So I have this code It's work when the image at my desktop, I added the image at src file put I couldn't convert it can you tell me what is the problem? this the code it set the image to fit the label too:
public void ScalImage() {
ImageIcon image = new ImageIcon("C:\\Users\\HP\\Desktop\\ath3.png");
Image img = image.getImage();
Image imgScale = img.getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_SMOOTH);
ImageIcon scaliedicon = new ImageIcon(imgScale);
jLabel2.setIcon(scaliedicon);
}
I tried to say: ImageIcon image = new ImageIcon("ath3.png");
didn't work
So I have solved it I wrote like that
ImageIcon image = new ImageIcon(getClass().getResource("ath3.png"));
It's work for me. I wish I'm right
I am trying to set a background image for a ListViewer.
listViewer = new ListViewer(parent);
listViewer.setContentProvider(this);
listViewer.setLabelProvider(this);
listViewer.getList().setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
Image image = new Image(Display.getDefault(), ListPart.class.getResourceAsStream("/icons/blurred.jpg"));
Rectangle imageBounds = image.getBounds();
Rectangle rectangle = listViewer.getList().getBounds();
GC gc = new GC(listViewer.getList());
gc.drawImage(image, imageBounds.x, imageBounds.y,imageBounds.width,imageBounds.height,rectangle.x,rectangle.y,rectangle.width,rectangle.height);
listViewer.getList().setBackgroundImage(image);
I tried to draw the image with GC but it doesn't work. If I leave the image as it is, it doesn't stretch after the List.
Is there an easier way to do this?
In JavaFX u can create a new Image from a string(path), how would i go about creating a new Image from an existing javafx.scene.image.image?
as following:
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);
But instead of the path an actual image object.
I want to change the size of the image.
There is typically no need to create a new Image instance in order to perform rescaling. The API allows you to view or draw scaled versions of an existing Image instance. For example, given
Image image = new Image("my/res/flower.png");
you can create an ImageView that displays a scaled version with
ImageView imageView = new ImageView(image);
imageView.setFitWidth(100);
imageView.setFitHeight(150);
or you can draw a scaled version to a canvas with
Canvas canvas = ... ;
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0, 0, 100, 150);
A late response, but since I ran into the same issue (I needed an Image to pass to setDragView() ), here is the solution I have implemented:
Image makeThumbnail(Image original) {
ImageView i = new ImageView(original);
i.setFitHeight(64); i.setFitWidth(64);
return i.snapshot(new SnapshotParameters(), new WritableImage(64, 64));
}
I have created BitMapSource from a list of RGBA pixels:
BitmapSource bmp = BitmapSource.Create(imageStrideInPixels, height, 96, 96, PixelFormats.Bgra32, null, imageData, imageStrideInPixels * pixelWidth);
I then create an image from the BitMapSource:
// create image and set image as source
Image BmpImg = new Image();
BmpImg.SetValue(Canvas.ZIndexProperty, 0);
BmpImg.Width = imageScaleWidth;
BmpImg.Height = imageScaleHeight;
BmpImg.Source = bmp;
I then add the Image to the Canvas:
mycanvas.Width = imageScaleWidth;
mycanvas.Height = imageScaleHeight;
mycanvas.Children.Clear();
mycanvas.Children.Add(BmpImg);
Canvas.SetLeft(BmpImg, 0); // to set position (x,y)
Canvas.SetTop(BmpImg, 0);
The problem is that it is not getting scaled to imageScaleWidth and imageScaleHeight, and it is being displayed half way down the canvas.
Note, I was able to do this in Java SWT by:
imageData = imageData.scaledTo(imageScaleWidth, imageScaleHeight);
gc.drawImage(imageData, 0, 0);
You can scale your image using a ScaleTransform:
// scale the original bitmap source
var transformedBitmap = new TransformedBitmap(
bmp,
new ScaleTransform(
imageScaleWidth / (double) bmp.PixelWidth,
imageScaleHeight / (double) bmp.PixelHeight));
// create image and set image as source
Image bmpImg = new Image();
bmpImg.SetValue(Canvas.ZIndexProperty, 0);
bmpImg.Source = transformedBitmap;
mycanvas.Width = imageScaleWidth;
mycanvas.Height = imageScaleHeight;
mycanvas.Children.Clear();
mycanvas.Children.Add(bmpImg);
Note that your image will be positioned at offset 0, 0 by default.
Instead of this
mycanvas.Children.Add(BmpImg);
Try this
mycanvas.Background = new VisualBrush(BmpImg);
This should render properly.
Are you sure the image is half way down the canvas and not the canvas itself is centered in its parent? I tested it and it appears that you can control the canvas position by setting vertical/horizontal alignment on canvas' parent. And also it scales properly when using the code you provided. However I created a BitmapSource in a different way. I used the following code:
PngBitmapDecoder decoder = new PngBitmapDecoder(new Uri(#"..."), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bmp = decoder.Frames[0];
I have a JavaFX tableview in which I'm listing files and their properties, including the ICON of the file. My problem is that a part of the icon is missing.
I don't know how to describe it completely so I included an image below.
My tableview is to the right and windows explorer is to the left.
Here is the code:
#Override
public void updateItem(ImageIcon imageIcon, boolean empty){
if (imageIcon != null){
// I have tried adding minimum width and height too
HBox box= new HBox();
// JavaFX scene imageview
ImageView imageView = new ImageView();
// Have tried with and without setFitHeight
imageView.setFitHeight(16);
imageView.setFitWidth(16);
// This line prints: 16 16
System.out.println(imageIcon.getIconWidth() + " " + imageIcon.getIconHeight());
// Create BufferedImage of the imageicon
BufferedImage bi = new BufferedImage(
imageIcon.getIconWidth(),
imageIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
imageIcon.paintIcon(null, g, 0, 0);
g.dispose();
// BufferedImage to fxImage
Image fxImage = SwingFXUtils.toFXImage(bi, null);
imageView.setImage(fxImage);
box.getChildren().addAll(imageView);
setGraphic(box);
}
}
Thanks.
UPDATE
I have google around some more and i copied an example directly and pasted into a new project, and the same thing happened with the icons. In the example, the user was probably using windows 7 by judging the look of the icons, so maybe this is just the way it looks in windows 8. It was the code in the answer at the bottom with a picture: www.stackoverflow.com/questions/28034432/