Can anybody give an example of exporting a ChartFX7 chart to SVG?
I've tried:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m_chart.setOutputWriter(new SvgWriter());
m_chart.exportChart(FileFormat.EXTERNAL, baos);
and :
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m_chart.setRenderFormat("SVG");
m_chart.renderToStream();
But both result in a null pointer exception.
The following successfully outputs to XML:
FileOutputStream fos = new FileOutputStream(Debug.getInstance().createExternalFile("chart.xml"));
m_chart.exportChart(FileFormat.XML, fos);
batik is a libary that you can import into your java libary to convert or create svg images. I dont know chartfx7 but that is the standard way to create svg in java.
Related
I have some issues that I am having a hard time solving.
I made a short code snippet :
BufferedImage image = ImageIO.read(new ByteArrayInputStream(payload));
BufferedImage thumbImg = Scalr.resize(image, Method.QUALITY,
Mode.AUTOMATIC, WIDTH, HEIGHT, Scalr.OP_ANTIALIAS);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Base64OutputStream b64s = new Base64OutputStream(baos);
ImageIO.write(thumbImg, DATA_TYPE, b64s);
return baos.toByteArray();
The returned thumbnail/byte is trimmed down. It deletes the bottom part and shows just a transparent area.
What I want is to have a scaled down image without removing some parts of it.
The purpose for this is to return a base64 to my html project.
Yea.. I just changed my logic for creating a base64 output.
Instead of having it write in Base64OutputStream of Apache Commons Framework.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Base64OutputStream b64s = new Base64OutputStream(baos);
ImageIO.write(thumbImg, DATA_TYPE, b64s);
return new ThumbnailPayload(baos.toByteArray()));
I did this instead
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(thumbImg, DATA_TYPE, baos);
return new ThumbnailPayload(Base64.encodeBase64(baos.toByteArray()));
Currently it is working. But if you guys can suggest another way with an explanation before the day ends, that would be awesome and helpful.
I need a dynamic solution to convert a unknown image format to .png in java.
Will .getType() help me out here, it seems only to return numbers.
The converted image should later be stored in a folder, but I guess that is easly done in the
ImageIO.write().
It's just that with converting an unknown image format that I have no idea how to approach.
This peace of code should do the magic:
File file = new File("unknown.type.pic");
ByteArrayInputStream bais = new ByteArrayInputStream(FileUtils.readFileToByteArray(file);
BufferedImage image = ImageIO.read(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
OutputStream outputStream = new FileOutputStream ("output.jpg");
baos.writeTo(outputStream);
Add missing try/catch/finally blocks.
I have array[Byte] of file, saved on DB.
Is there a way to convert it to JPG if its for example PNG ?
And no saving file on disc, just operations on those array[byte]
Thanks!
Read it into a BufferedImage and write it again to a byte array, using JPG encoding.
InputStream is = ...;
BufferedImage img = ImageIO.read(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "JPG", baos);
byte[] array = baos.toByteArray();
For the inputstream (is), use either a inputstream that comes straight from the BLOB in your DB, or use a ByteArrayInputStream.
To process some images in my android application I currently use code like this:
FileOutputStream fileOuputStream = new FileOutputStream(imgpath);
[..DO SOME STUFF..]
Bitmap data = BitmapFactory.decodeByteArray(bFile, 0, bFile.length, options);
data.compress(Bitmap.CompressFormat.JPEG, 90, fileOuputStream);
[..DO SOME STUFF..]
File file = new File(imgpath);
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
[..DO SOME STUFF..]
file.delete();
//NOTE: The code is all in the same method
the problem is that passing my image from one part of the code to another using this method creates a temporary file.
I was looking for a way to read / write the file data using a memory variable, something like "generic stream" in which store data in order to replace use of "FileInputStream " and "FileOutputStream " and do not write temporary file.
If you are able to use an InputStream or OutputStream you can use ByteArrayInputStream or ByteArrayOutputStream for in memory handling of the data.
If you have two thread you can also use PipedInputStream and PipedOutputStream together to communicate between the threads.
You could write your data to a ByteArrayOutputStream and use the byte array of that stream:
ByteArrayOutputStream out = new ByteArrayOutputStream();
data.compress(Bitmap.CompressFormat.JPEG, 90, out);
// now take the bytes out of your Stream
byte[] imgData = out.toByteArray();
I am trying to compress a sequence of images in png format. It seems that compression is going well:
FileOutputStream fos = null;
GZIPOutputStream gzip = null;
fos = new FileOutputStream(PATH_SAVE_GZIP);
gzip = new GZIPOutputStream(fos);
for (int i = 0; i < NB_OF_IMAGES; i++) {
BufferedImage im = images.get(i).getBufImg();
ImageIO.write(im, "JPEG", gzip);
}
gzip.finish();
gzip.close();
fos.close();
However I get Exception Nullpointer... when I try to uncompress it with this code.
What am I'm doing wrong?
I've finished my project and now I know the answer. This could be solved by several ways:
One is by using ObjectOutput/Input Stream and write BufferedImages like objects.
The other is to use ByteArrayOutputStream and write images like bytes. Prior to use this you should know the size to be written. So I've solved this writing size before each image. Not efficient way... However works.
fileOutputStream fos = new FileOutputStream(path);
GZIPOutputStream gzip = new GZIPOutputStream(fos);
gzip.write(shortToBytes(numImatges));
gzip.write(shortToBytes((short) 0));
for (int i = 0; i < dates.getNB_OF_IMAGES(); i++) {
if (images != null) {
im = images.get(i).getBufImg();
}
ByteArrayOutputStream byteOstream = new ByteArrayOutputStream();
ImageIO.write(im, "jpeg", byteOstream);
byteOstream.flush();
byteOstream.close();
gzip.write(shortToBytes((short) byteOstream.size()));
gzip.write(byteOstream.toByteArray());
}
//close streams
Your problem is that you write all the images to a single GZIP stream and when reading, ImageIO doesn't know where one image ends and the next begins.
You got two options:
Use ZIP instead of GZIP
Package the files in a TAR file using jtar or Java Tar Package and then GZIP the tar, when reading you will first UnGZIP and then extract the images from the tar file