Encoding Images Into Video with JCodec Errors - java

I need to encode multiple images (Of which I have the complete path) into a video of a certain FPS on android.
Trials:
How to create a video from an array of images in Android?
Why I couldn't get it to work:
I added the Jcodec Dependecy to gradle file (
compile 'org.jcodec:jcodec:0.2.3'
compile 'org.jcodec:jcodec-android:0.2.2'
)
I then pasted the code into a function and this is what I get:
As You can see I managed to import SequenceEncoder (import org.jcodec.api.SequenceEncoder;)
But it doesn't recognize Buffered Image (I think it's because I have to use Bitmap)
And it gives me an error in the SequenceEncoder.
Also doesn't recognize the encodeImage Method.
Then I tried with the code I found on JCodec webSite:
SeekableByteChannel out = null;
try {
out = NIOUtils.writableFileChannel("/tmp/output.mp4");
// for Android use: AndroidSequenceEncoder
AWTSequenceEncoder encoder = new AWTSequenceEncoder(out, Rational.R(25, 1));
for (...) {
// Generate the image, for Android use Bitmap
BufferedImage image = ...;
// Encode the image
encoder.encodeImage(image);
}
// Finalize the encoding, i.e. clear the buffers, write the header, etc.
encoder.finish();
} finally {
NIOUtils.closeQuietly(out);
}
But it doesn't recognize AWTSequenceEncoder and thus the methods encodeImage and finish.
What am I doing wrong?

Ok, I found the Answer to the problem, technically it is in the answers of this question:
How to create a video from an array of images in Android?
But has only two votes, despite being the only one that worked for me and for what I found out the only one that should work. You cannot use BufferedImages in android, while the most voted quesion do and the SequenceEncoder that I didn't find is replaced with AndroidSequenceEncoder.

Related

PDF Box getXObjectNames() does not recognize bar code on my PDF, however it does recognize it on a PDF file I got off the internet

I am trying to fetch and read bar codes from my PDF using getXObjectNames() of PdResources.
My code is very similar to this link: https://issues.apache.org/jira/browse/PDFBOX-2124
If you see the above JIRA item, you will see a PDF file attached to it.
When I run the code on that PDF file I get the desired output (i.e. the bar code type is printed.)
However when I run it on my PDF, it does not recognize the bar code in it. (I have checked that the bar code is in fact an image and not text.)
Also it may sound weird, but it did work on my PDF once and I haven't made any changes since then, but it definitely does not work now. (I cannot share the PDF for some reason.)
Has anyone faced a similar issue?
Also this is my first question on Stack Overflow. Please tell me if I am wrong anywhere.
Here is a link to that pdf:
https://drive.google.com/file/d/1PzVApIePg4U9XL399BpAd2oeY6Q2tLEB/view?usp=drivesdk
In General
As you don't show your code but only describe it as very similar to that in PDFBOX-2124, and as you say you cannot share the PDF for some reason, I only have that code to analyze. Thus, I cannot tell what really is the issue but merely enumerate some possible problems
First of all, that code only inspects the immediate resources of the given page for bitmap images:
PDResources pdResources = pdPage.getResources();
Map<String, PDXObject> xobjects = (Map<String, PDXObject>) pdResources.getXObjects();
if (xobjects != null)
{
for (String key : xobjects.keySet())
{
PDXObject xobject = xobjects.get(key);
if (xobject instanceof PDImageXObject)
{
PDImageXObject imageObject = (PDImageXObject) xobject;
String suffix = imageObject.getSuffix();
if (suffix != null)
{
BufferedImage image = imageObject.getImage();
extractBarcodeArrayByAreas(image, this.maximumBlankPixelDelimiterCount);
}
}
}
}
(PDFBOX-2124 PdPageBarcodeScanner method scsan)
Bitmap images can also be stored elsewhere, e.g.
in the separate resources of form xobjects, patterns, or Type 3 fonts used on the page; to find them one has to inspect other page resources, too, even recursively as the image might be a resource of a pattern used in a form xobject used on the page;
in the separate resources of annotations of the page; thus, one has to recurse into annotation resources, too;
inlined in some content stream; thus, one also has to search the content streams of the page itself, of page resources (recursively), and page annotations and their resources (recursively).
Furthermore, the bitmap might be given in some format (in particular with some colorspace) which PDFBox does not know how to export as BufferedImage.
Also the bar code may be constructed using some mask applied to a purely black bitmap in which case your code probably only tries to scan that purely black image.
Furthermore, you say
I have checked that the bar code is in fact an image and not text.
If you only checked that the bar code is not text, it may not only be a bitmap image but it can also be drawn by vector graphics instructions. Thus, you also have to check all content streams for vector graphics instructions drawing a bar code.
Also there may be combinations, e.g. a soft mask of vector graphics may be active when drawing a purely black inlined bitmap image etc.
And I'm sure I've missed a number of options here.
As next step you may want to analyze the PDF you cannot share to find out how exactly that barcode is drawn.
Alternatively, you render the page as bitmap image and search that large bitmap for bar codes using zxing.
Sample PDF.pdf
You provided a link to a sample PDF. So I tried to extract the bar code using code very similar to that from PDFBOX-2124. Apparently the code there was for some PDFBox 2.0.0-SNAPSHOT, so it had to be corrected a bit. In particular the method getXObjectNames() you mention in the question title finally is used:
PDResources pdResources = pdPage.getResources();
int index = 0;
for (COSName name : pdResources.getXObjectNames()) {
PDXObject xobject = pdResources.getXObject(name);
if (xobject instanceof PDImageXObject)
{
PDImageXObject imageObject = (PDImageXObject) xobject;
String suffix = imageObject.getSuffix();
if (suffix != null)
{
BufferedImage image = imageObject.getImage();
File file = new File(RESULT_FOLDER, String.format("Sample PDF-1-%s.%s", index, imageObject.getSuffix()));
ImageIO.write(image, imageObject.getSuffix(), file);
index++;
System.out.println(file);
}
}
}
(ExtractImages test testExtractSamplePDFJayshreeAtak)
The output: One bitmap image is exported as "Sample PDF-1-0.tiff" which looks like this:
Thus, I cannot reproduce your issue
PDF Box getXObjectNames() does not recognize bar code on my PDF, however it does recognize it on a PDF file I got off the internet
Obviously getXObjectNames() does return the name of the bitmap image xobject resource and PDFBox exports it just fine.
Please check with your code whether as claimed the image is not extracted or whether some later step simply cannot deal with it.
If in your case indeed the image is not extracted,
update your PDFBox version (I used the current development head but the newest released version should return the same),
update your Java,
check whether you have extra JAI jars that might cause trouble.
If in your case the image is extracted but not analyzed as expected by later code,
debug more thoroughly to find out where the analysis fails,
create a new question here focusing on the QR code image analysis,
and provide enough code and the tiff file to allow people to actually reproduce the issue.

How to analyze a photo sent through JSON

I have a Web Service that takes a photo through a POST statement and returns a modified copy of that photo back. We are making changes to the way it processes the photo, and I want to verify that the photo at least has different properties coming back than it did before our changes went into effect.
The photo is being returned as a byte stream inside one of the fields of a JSON object. I can analyze the JSON object pretty easily, but I'm trying to figure out how to get the byte stream into an Java image object so that I can get its dimensions.
Possible duplicate of this question
... I'm trying to figure out how to get the byte stream into an Java image object so that i can get its dimensions.
I'd suggest using a BufferedImage in the following format/snippet. Note: I load my image in from disk for the example and use try-with-resources (which you may revert to 1.6-prior if needed).
String fp = "C:\\Users\\Nick\\Desktop\\test.png";
try (FileInputStream fis = new FileInputStream(new File(fp));
BufferedInputStream bis = new BufferedInputStream(fis)) {
BufferedImage img = ImageIO.read(bis);
final int w = img.getWidth(null);
final int h = img.getHeight(null);
}
You can use:
OS Process Sampler and 3rd-party tool like ImageMagick
JSR223 Test Elements, to wit
JSR223 PreProcessor to get information on the photo, you're trying to upload
JSR223 PostProcessor to get information on the photo, returned by the Web Service
JSR223 Assertion to compare two photos
Depending on what parameters you need to compare you can use ImageIO API (out of the box, bundled with JDK), Commons Imaging, ImageJ and so on.

ZXing library can't decode Datamatrix barcode

I'm trying to use ZXing library to decode Datamatrix barcode. Here are my code sample:
BufferedImage bi = img.getBufferedImage();
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
LuminanceSource source = new BufferedImageLuminanceSource(bi);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
DataMatrixReader dataMatrixReader = new DataMatrixReader();
try {
Result res = dataMatrixReader.decode(bitmap,hints);
System.out.println("resultText = "+res.getText());
} catch (Exception e) {
System.out.println("failed to get resultText");
e.printStackTrace();
}
I've seen almost the same samples many times accross https://stackoverflow.com/ and other sites, but this approach does not working for me in this form.
As a source I'm using images grabbed from IR-camera. Here are example image:
As you see, the barcode is almost exactly at the center of an image, as Sean Owen recommended here and here. If I programmatically convert this image to black&white and crop image to bound barcode with some white space around it only, then ZXing works perfectly with images like this. But the problem is that barcode in real could have little deformations, so my simple algorythm can't help me to crop image properly. More over barcode could be placed not exactly in the center of an image and cold have a little bit different brightness. I saw threads mentioning OpenCV capabilities to find out placement of speciects objects on the image, like this one, but they are quite old. Is something changed since then? And what should i yet certainly consider to write 100% reliable datamatrix decoder (and detector) in my specific situation?
I decided to supply LuminanceSource and BinaryBitmap images made of .toString() text output of correcponding objects for reference:
http://s28.postimg.org/l53sykhx9/Binary_Bitmap.png
and /65z0vlbpl/Luminance_Source.png (at the same domain). They are looking good and ready for decoding, but what is wrong with decoding then.
After all this image and similar ones recognized and decoded very well with smartphone software and i'm just wanted achieve same results.
you need to enable it from settings programmatically or manually.
in class DecodeThread.java you can see the line that enables data matrix encoding
decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);

Is there a way to decode a .ICO file to a resolution bigger than 16x16?

I'm working on android and trying to download and display a favicon(.ICO) from a website on an ImageView.
So far I've manage to read the .ico from a website using an HTTP connection, retrieve it as an InputStream. Then I use a BitmapFactory to decode the stream into a Bitmap and display it on the ImageView. Here's the code:
public Bitmap getBitmapFromURL(URL src) {
try {
URL url = new URL("http", "www.google.com", "/favicon.ico");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap myBitmap = BitmapFactory.decodeStream(input, null, options);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
The problem is that the decoding of the inputStream always returns a small 16x16 Bitmap. If I well understood, a single .ICO file can store different image resolutions, like 32x32 and 64x64. My question is, is there a way to decode the 32x32 or the 64x64 Bitmap instead of the 16x16?
Also, if there isn't a solution with BitmapFactory, is there a library or java code to do this?
NOTE: I don't want to resize the Bitmap, I want a 32x32(or bigger) resolution without losing the image quality by stretching.
Thanks in advance.
I know the question was asked 3 years ago, but I have faced the very same problem and adapted a portion of image4j into ico4a, which you can find here: https://github.com/divStar/ico4a (I wrote it myself because I wanted to load the biggest image from a favicon; since image4j uses AWT-classes, which are not easily available for android, I made it so ico4a mostly uses native android classes).
It can decode most valid ICO-files into a List of Bitmap-objects. I believe either the library itself or the sample application has also a method to retrieve the biggest Bitmap-object.
However, my library is not able to write ICO-files; it can only read them. You can save the individual images as PNG or JPEG graphics easily though using some built-in android functionality.
The favicon file may contains mutliple icons, usually one in 16x16 and one in 32x32. This is discussed in the following thread : How to have multiple favicon sizes, yet serve only a 16x16 by default?
It is the case with the Google's favico. If you try to download the file www.google.com/favicon.ico and open it with an application like IrfanView, you can see that there are two icons (16x16 and 32x32).
To answer your question, you should use a proper library to extract multiple icons, like image4j, and choose the one you need.
http://image4j.sourceforge.net/

how to Show or Read docx file

I am new to rendering files in android, and I want to render or display a docx file in my application.
I had already extract text from docx file, but now I want to extract images from the docx file as well.
I've found several ways to display images in pure Java, but are there any good examples for Android?
I tried this code to fetch Images but not working...
public void extractImages(Document xmlDoc)
{
NodeList binDataList = xmlDoc.getElementsByTagName("w:drawings");
String fileName = "";
Node currentNode;
for(int i = 0; i < binDataList.getLength(); i++)
{
currentNode = binDataList.item(i);
if(currentNode.getNodeType() == Node.ELEMENT_NODE && ((Element)currentNode).hasAttribute("w:name"))
{
File newImageFile = new File(picDirectory, ((Element)currentNode).getAttribute("w:name").replaceFirst("wordml://", ""));
if(newImageFile.exists())
{
}
else
{
if(writeImage(newImageFile, currentNode))
{
//Print some success message
}
}
}
}
Have a look at AndroidDocxToHtml, which I made to demonstrate using docx4j on Android.
A couple of caveats.
First, that project does not include all docx4j dependencies, only the ones required for docx to HTML conversion. So if you want to do other things, you may need others of the dependencies.
Second, docx4j requires JAXB - see this blog post re JAXB on Android - and JAXB context init on app startup takes a while depending on the device. There are ways to work around this, but at extra effort.
If all you want to do is extract the images, and you don't care how they relate to the text, you could just look for image parts. You might use OpenXML4J for that, and avoid JAXB.
The easiest way to create an image in Android is to use the BitmapFactory factory methods.
The BitmapFactory class has methods for creating a Bitmap from a byte array, a file or an InputStream.
Once you have a Bitmap object you can display it by setting it on an ImageView in your layout using the setImageBitmap method.
You can just unzip the file (rename to .zip and open it) then you can investigate the folder structure, where the images are located etc.

Categories

Resources