JavaFX: get BufferedImage from path - java

Coming from AWT/Swing, I've started experimenting a bit with JavaFX the last few days. I realized that what I used to do in thousands lines of codes can now be done in a few hundred.
One problem I came across is, however, the following: I'm trying to develop a small painting app where the user can choose brush size and color for its strokes. For all the strokes the user makes, I use the JavaFX class Path and add these paths to a Group (which is added to a Pane) where they are - automagically - painted. Now I want to store the resulting image as a jpg and try to raster all the paths in a BufferedImage. However, I found no functions in the API that help me do that.
I tried to use Canvas and its GraphicsContext, but that did not help. How could I raster all the JavaFX Paths from a list on an image?

Take a snapshot of your Group to get a JavaFX Image.
Use SwingFXUtils to convert your JavaFX image snapshot to a buffered image.
Use ImageIO to convert your buffered image to a jpeg, png, etc

Related

Jpeg to Svg and Image Tracing

Currently we have a requirement where we have an image depicting the blueprint of the mall (red specifies the booked up areas and white specifies the available areas) and the image is available in a raster (JPEG) format.
We would like to drag and drop some icons onto the available areas of the image (in white). There should also be zoom in and zoom out functionality to be given for the above image as well
Since the JPEG has a lossy scaling, zooming after a certain limit can result in a jagged image. One proposed solution is to convert the image to SVG (Scalable Vector graphics).
Going with the expanded form of SVG, it simply tells us that image is:
s=>scalable (i.e. you can zoom to any level without compromising the quality)
v=>vectorized (i.e co-ordinates are available)
So by simply looking at the XML format of the image, we can predict whether to allow dropping an object at fill=red or fill=white where red and white are the two colors in the image. This might not be appropriate solution, but I'm just guessing it this way
Now the problems I see with this approach is:
Converting an image with some open source tool (InkSpace) - if we trace it with ink-space, which uses portace inside it to trace the image, it can handle only black and white colors.
Note-: Most of the tools comes with some license.
Problem with inkspace is that it embeds the image into the SVG map and does not create the co-ordinates. If you trace it with inkspace, it only creates the outline of the image.
Converting it with some online utility - Not recommended in our case, but doing so results in a large size of the SVG image. For a 700 KB file, the SVG generated is about 39 MB, which when opened up on a browser crashes the browser.
Most of the time when the image is converted to an SVG, it becomes way too large a big factor to worry about. There are utilities available like Gzip to compress files, but this is a two way route - first you convert, then you compress.
Using delinate (which employs a portace and autotrace engines in it) - the quality of the image produced is not good.
Using Java code - Again the quality suffers. Java graphics are not fully developed to handle the conversion (size is again way too large)
Converting the image to PDF, then to SVG - this also embeds the image into the SVG file, which is useless as no co-ordinates are available
Does anybody got any idea on this ,how to deal with this situation?,Can we handle the drag and drop on raster(jpeg,png...etc) images itself?
Thanks
Dishant Anand

Create image from text with unknown number of lines

I would like to convert a string of text into an image. The issue is, I want the text to wrap if it is wider than the length of the image, and the height of the image to be dynamically sized to perfectly fit the text, so that I know how much space the text takes up.
I'm working in Java and there are several things I have tried:
Rendering HTML in a JPanel and saving as a BufferedImage. The problem here was that most of the css I used was ignored by the JPanel and the image was unusable.
Using ImageMagick and img4Java. The two big failures with this solution was that I needed the command-line tool installed, which I can't do on our server. The second was that I couldn't easily convert the image to buffered image for use in the rest of the app.
Does anyone know a way to do this in Java?
Thanks!
In this example, an arbitrary panel is rendered into a BufferedImage and displayed in an adjacent panel at half-scale. The example uses a grid of labels, but you can use the wrap feature of JTextArea or the geometry supplied by TextLayout, examined here.
You might use a label containing HTML for the line-wrap, as shown here.
To get an image of that, see LabelRenderTest.

How to check runtime that is there any changes in image(16bit,gray-scale) in myApp using java?

I'm making desktop app in Java Swing.
In my app I do some image processing on my image which is a 16 bit, Gray-Scale and tiff image.
In my app the user can open images from tree using drag and drop of image into a JDesktopPane.
Now when user done some process on image like Remove Noise or set Contrast, when they close the image my app should ask if they want to Save Changes in Image?
So how can i check run time that some changes in Original image?
The java.awt.image.Raster contained in a BufferedImage does not override Object#equals(). This is largely because iterating over w * h pixels can get expensive: O(wh). Any optimization depends on the nature of the change. If you're only looking for global changes, such as noise or contrast, comparing a number of samples may suffice. You'll also want to profile your intended usage.

Toolkit.getDefaultToolkit().createImage() vs ImageIO.read()

I'm creating a UI using Swing and I want to display an image in a JLabel. The code I use is the following:
JLabel label = new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg"))));
This works fine if I use png images but when it comes to jpg (only some of them), I get a redish image (a different one than the one I see in Paint.NET).
The image I used is this one: img.jpg
So I tried (as an alternative):
Toolkit.getDefaultToolkit().createImage(new File("img.jpg").getAbsolutePath());
Does anyone have an idea of why this happening? Is it a special JPEG format which is not supported?
I've read on this forum that most people recommend to use ImageIO (here for example). Why?
Thanks a lot
As discussed here, your JPEG image may contain spurious transparency information. One simple expedient is to render the image in a buffer having a compatible color model, as shown here.
It looks like you have found a bug in ImageIO.read... (I can reproduce the red tint, and it is definitely not how it should look like).
You can try to
save the JPEG files with other settings
open/re-save the file with other programs (hoping to get a more common JPEG-encoding)
or use the Toolkit method (if you don't control the images).
The only problem with the Toolkit method is that the getImage() method returns immediately after it is invoked and the loading is happening on a background thread, so you cannot start working with the Image object immediately.

How to use BuffereImage for a Chess Game

I am trying to create a chess game for java using images my classmate and I are working on. We are using a created image to serve as the board and pieces to place about that the user can drag and place. Unfortunately we do not currently know how to import images from a file directory into the program itself. Not only that, but we are completely clueless as to why this would occur, as the Java API guide does not clearly explain this process.
The portion of the JDK API that provides image input is the package javax.imageio. The easiest way to load an image (into a BufferedImage object) is:
BufferedImage img = ImageIO.read( new File(pathname) );
Don't forget to catch the appropriate exceptions.
Read the section from the Swing tutorial on How to Use Icons for working examples of different ways to read images and display them on a label. Its easier to drag a label around the screen then just and image.

Categories