How can I create a BufferedImage from an SVG path? - java

I have attempted to find this answer (on Google and Stackoverflow) without success, but I'm sure it must have been asked before, so feel free to point me onwards to the answer if it exists.
Currently, I have a Java servlet that loads a PNG from disk into a BufferedImage, writes text on top of it, and then streams back the byte[] to the client.
My desire is to replace the PNG-from-disk with a rendered SVG path, from a collection of icon-paths that I've source online (e.g. "M21.871,9.814 15.684,16.001 21.871,22.188 18.335,25.725 8.612,16.001 18.335,6.276z" and "M22.727,18.242L4.792,27.208l8.966-8.966l-4.483-4.484l17.933-8.966l-8.966,8.966L22.727,18.242z").
I've come across Batik and SVG Salamander, but am struggling to understand how I would accomplish the above with either of them, most specifically, how to render the SVG path into the BufferedImage. I need to be able to specify (a) the dimensions of the image, (b) the fill-color and (c) & (d) the stroke width and color.

Here is an example that basically uses the Transcoder API.

Related

Java Drawing shapes to file of 2D object

I have a play framework application which I want to be able to produce a product label from. I have the label design in illustrator. It consists of a black circle, white writing with a QR code in the middle, also has curved text.
I want to create a high resolution PDF and/or image file of this design on the fly. All most all of the drawing stuff I find for java relates to swing.
Anyone done this?
The basic class which allows creating an image programatically is BufferedImage and the corresponding Graphics2D class. You are not forced to use it with Swing. You can easily convert it to common graphic formats like PNG. Then you can save it as an image file or place it in a generated(e.g. with iText) PDF.
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
In other words - yes, it can be done.
But if I ware you I would consider exporting the design from Illustrator to a file and use it as a resource in your application. But if you need to scale it programatically you ought to consider using SVG format to avoid loosing quality. Java does not have build-in support for vector images so you should look at
Apache Batik

Embed Graphics2D into itext without absolute positioning

I'm creating PDFs with iText (LGPL) which include some Text and self-drawn (Graphics2D) images.
My current solution is to draw the images on a BufferedImage and then include it in the PDF, which has several drawbacks:
If printed, the images just look ugly, a way to circumvent this is to use larger images, and with 3000*3000 it looks ok. But this leads to the next problem: time. It takes several seconds to compress one image (I haven’t found a way to disable it, and files would be huge without compression).
PdfGraphics2d from iText looks good, but has one major drawback: it’s only able to draw to the background of the PDF, and there seems to be no way to wrap it in some kind of element.
Is there a way to draw on a PDF without having to use absolute positions? I’m using Graphics2d because it’s used also to provide a preview in the UI.
You can wrap a PdfTemplate inside an Image object without losing any of the vector image's quality. In most cases, you'll use the Image object to add raster images to a PDF document as an Image XObject. However, in this case, the PdfTemplate will be added as a Form XObject using its original vector data. Another situation when this happens, is when you add a WMF file; such as file is converted into PDF syntax automatically.

IllegalArgumentException: Numbers of source Raster bands and source color space components do not match For a color image

I got this exception when my app is trying to read a JPG image using ImageIO.read( ) method. This exception is not thrown for all jpg file.
I found this answer useful Intersection of bands in R raster package, but still it converts my colored image to a black n white one. I guess that question has focused on the right issue, but I want it to keep my image colored(not black and white).
Download image: https://skydrive.live.com/?cid=19547371C4F3B839&id=19547371C4F3B839%21105
Simply if someone can obtain a BufferdImage from the image given that's enough (should not convert the image to gray scale one).
You are a genius if you can answer this :D.
Plz help.
That is typically the exception you get when Java cannot read the JPEG file. While they are standard compliant JPEGs Java has not implemented the full standard. I recommend converting the JPEG file with ImageMagick, Irfanview, or something like this before actually trying to open it in Java.
You may use ImageJ, which can deal with most JPEGs.

How to make a SVG map in android? (and place markers on the map)

I'm making an app for a real life game. The app needs to use a custom map that uses scalar vector graphics (SVG). The map I'm using provides very accurate detail, such as door locations inside a building; This is why i'm using SVG instead of the maps api.
Now I know there is an api for svg (http://code.google.com/p/svg-android/), so here is what I need help with:
1) The image must be zoomable. The only reason for using the SVG graphics is for clean zooming.
2) I need to place dynamic markers (images, or buttons w/ numbers) on the SVG image. An SQL table has an image, and a location. The table will be updated, added to, and removed from. I need to place said images on top of the SVG image, and possibly each other. Coordinates and math aside, how do I place the images on top of each other (can't use XML since they aren't static).
I need to use Android 2.2
The default browser in 2.2 doesn't support SVG. You can target Opera Mobile, which supports SVG and other new technology much better, but it won't be a native app.
The first step I'd try would be designing the interface in Inkscape, then you can reference the .svg file from HTML and move elements with javascript.
flying-pigs,
If i understand your question, you want to display a SVG like that on your phone ? So you can display user location on that map.
I describe a working solution here : Add SVG Tile Provider
Let me know if you have other questions about GG Map

Tiles for tiff images in java ? or java wrapper for libTiff?

I am converting pdf to tiff images for one of my project.
Than I using iipserver to generate tiles for the tiff images on fly. But this process is killing my CPU.
so I thinking of generating tile in advance and showing them directly instead of using iipserver. I researched into iipserver and got this libTiff c++ utility which is doing tiling work for the same server.
So I wanted to know is there any java wrapper for this libTiff or are there any other method from which i could generate tiles directly from tiff image or directly from pdf pages to tiles?
ImageJ can handle Tiled Pyramidal TIFF. JAI can handle MipMaps generated from TIFF files as well.
And if you're looking for a ready-made solution, take a look at djatoka.
In the end , I got the solution. There are few points that I want to explain
1) Tiles parameter in any tiff image is just a metadata value, so
there is nothing physically marking of tiles in an image
That best way I found to generate tiles out of image is BufferedImage class method:
bufferedImage.getSubimage(x, y, w, h)
Now play with this method in a loop for image matrix as per your needs.
It worked 200% perfect for me.. cheers to all :)

Categories

Resources