Fastest way to convert to jpg? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an image in a byte[] variable and my goal is to convert it to JPG format and then create a BufferedImage variable from it.
Speed is very important here. Just to create a BufferedImage from the byte[] on a 500kb image takes 0.5 seconds.
One approach is (but very slow) is:
create a BufferedImage from the image byte[]
use ImageIO.write to convert the image to jpg and write it to disk
read the image from disk and create a BufferedImage from it
Is there any faster way to do this, please?
edit:
The byte array contains the content of a valid PNG, JPG or GIF image i have read the HDD.

There's a library called OpenCV. It's a C/C++ library but it also has support for Java through a JNI wrapper. I have to mention that OpenCV is specialized in computer vision and general image processing and not so heavily in compressing images and having rocket science methods to write images to disk.
A JPG image of 500 kB is probably much larger in raw data when you read it into memory.
Honestly, if I were you, I'd stick with the stuff that's already there in the Java API (such as ImageIO).
If you plan on processing loads of images, you could create a list of the files you wish to convert to a different format, design your application be multi-threaded and separate the tasks over multiple threads. That's really the way to win back time.

Related

Move C++ memory without copy [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Currently I am working on transfer image from C++ to Java.
The destination location is allocate by Java,
the source location is the image generated by C++, so.
I have a
uint8_t* pixelPtr
, I want to move the content of this to a
__uint8_t* data
without copy.
I have 1920*1080*3 bytes in total, so I want to move rather than copy to be fast in computation, I am wondering is there any trick way to do so?
Thank you in advance!
Let's recap:
The source is a buffer allocated in C++ by an image generation function.
The destination is a buffer allocated in Java by some other code somewhere.
You want to transfer data between the two buffers.
As long as those two buffers are distinct, there is no "trick" to avoid this. "Moving" in this context would mean swapping the pointers around, but that does nothing to the underlying buffers. You will just have to copy the data.
Explore solutions such as generating the data in the destination buffer in the first place, or making use of appropriate functionality exposed by the C++ image generation function (or the Java code). Unfortunately we can't speculate on the possible existence or form of such solutions, from here.
The standard way is, you should modify your C++ code so it creates the data not wherever it wants, but in the given place. That is, if you have code like this
uint8_t* GenerateImage(...parameters...)
{
uint8_t* output = ... allocate ...
return output;
}
you should change it to receive the destination as a parameter
void GenerateImage(...parameters..., __uint8_t* destination)
{
... fill the destination ...
}
The latter is better C++ design anyway - this way you don't need to make a separate DestroyImage function - the memory is managed entirely by Java.

Combining pdfs into one pdf [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a web app that I am changing to use xPressions from EMC2. There is a point where xPressions returns a pdf document inside a java servlet. Before we added xPressions, we would combine several of these pdfs into one large pdf and send it back to the user/screen. But xPressions can only process one pdf at a time. It is returning the pdf as a byte[] array. So I am trying to find a way to take the byte[] arrays and combine them into one large pdf to send back to the user/screen. Before we had xPressions we were using an old version of Big Faceless (bfo.com) to combine the individual pdfs into one pdf in the servlet. I have not been able to get the byte[] array to a valid pdf using the old bfo.com software. I have searched on Google and here on stack overflow for another technique. I have found answers that are close but most are using Linux or c#. Also, these pdfs are created inside the java servlet and are not existing on a hard drive where I could read them in and convert them. I have to take the byte[] array and work with that. So, does anyone have any ideas for me ? Thanks in advance !
You can use PDFBox for merging your pdf files. PDFMergerUtility class has a method addSource which takes in an inputstream, you can convert the byte array to inputstream and add that as a source.
PDFMergerUtility merger = new PDFMergerUtility();
merger.addSource(...);
merger.addSource(...);
merger.setDestinationFileName(...);
merger.mergeDocuments();

What is the fastest way to transfer data between C++ and Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a C++ program and a Java program. On the C++ side, I have a vector having millions of entries. I need to transfer this data to my Java program. So far, I have tried the following:
Created a unix socket, converted the vector to a long string (Serialized) and send it through the unix socket
Created a thrift server/client model to transfer the data
Both approaches worked very well, but the performance I'm getting is quite low. I don't even see it using the full network bandwidth (in the case of thrift).
Also with the unix socket approach, Since I'm serializing it to String and then again converting this string back to a string array (received byte[] to String and split) on the Java side is very expensive operation.
What is the best way to transfer data faster from the C++ world to the Java world with lesser overhead on reconstructing/serializing the object?
If both problems are on the same machine, I would use a shared memory mapped file.
This way both programs can access the data at full memory speed without serialization/deserialization esp if the values are int, long or double values.
You can place the file on a tmpfs, or ram drive to avoid hitting a hard drive.

Java BufferedImage: one image or many? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a game, and I am using various tilesets. Suppose for example that my tileset is 25x25 (meaning each image is 25px wide by 25px high), and I have 16 images. I can either use one 100x100 image and use the BufferedImage#getSubimage method, or I can create 16 25x25 .png files. The first answer would save on time since I wouldn't need to create the 16 files required, but the second method would be clearer since I can name each of my files something obvious.
I'm mostly worried about performance; is it better to keep several small images in memory, or one large file?
Has anyone tried both methods? If so, which did you find worked best?
People generally go with spritesheets/tilesets as this cuts down on size and it's not really much a performance hit to just grab the image you need based on its position and size. For a 2D game with pixel graphics, performance isn't generally much of a concern anyway.

How to compare two PDFs based on visual differences programmatically? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to compare and get all the visual differences in the two PDF files. I know there are some questions related to this on stack overflow but they are not fulfilling my need.
I'm currently using PDFBox to generate images for pages in PDF and comparing the bytes of the images.
By this approach I'm able to know that particular page is differing.
But I need to find to know some more fine details such as font size of some text, for say - "The text" is differing in the page number, say 6 in the PDFs.
Not only for text but I need to take care of all the visual differences such as images, text in the charts etc.
Please suggest me someway to achieve this.
PS: I tried using Apache Tika but I'm getting the sense that it could be used to get structured text in XHTML and metadata. But I'm seeing the fine details such as font size, font eight is not appearing in structured text. Please correct me if I'm getting it wrong.
PDF to image using Java
Convert PDF to thumbnail image in Java (there's an example of pdf-renderer use here)
https://www.google.com.br/search?q=PixelGraber&ie=utf-8&oe=utf-8&rls=org.mozilla:pt-BR:official&client=firefox-a&gws_rd=cr&ei=K1PhUqD2Jei0sQTQs4DoAw
A good library for converting PDF to TIFF?
Convert jpeg/png to an array of pixels in java
int pixels array to bmp in java
Finding pixel position
Get Pixel Color around an image
For extraction of text using PDFBox: Extracting text from PDF file using pdfbox
There are classes in PDFBox for detecting font position, type, size and maybe (didn't search deeper) other settings. (Links below) You could, then, extract text from both PDFs, compare them to check if texts are equal, then - if they are equal - compare their format. If there's something different, mark for display into another text, image or PDF.
http://pdfbox.apache.org/docs/1.8.3/javadocs/org/apache/pdfbox/util/TextPosition.html
http://pdfbox.apache.org/docs/1.8.2/javadocs/org/apache/pdfbox/pdmodel/graphics/PDFontSetting.html
Check out this Java package: https://java.net/projects/pdf-renderer
You can convert the pdf to an image and then traverse the image as a 2D array and compare differences like that.

Categories

Resources