Pass an Image from Flex to Java: BufferedImage == null - java

I want to pass an image (screenshot of a graph) from Flex to Java over BlazeDS. The goal is that the image will be stored on the serverside.
This is my Flex routine (called after clicking a button):
protected function btn_clickHandler(event:MouseEvent):void
{
var snapshot:ImageSnapshot = ImageSnapshot.captureImage(graphViewStack.selectedChild,);
var balanceSheetImage:String = ImageSnapshot.encodeImageAsBase64(snapshot);
//Testing the image
var base64Dec:Base64Decoder = new Base64Decoder();
base64Dec.decode(balanceSheetImage);
generatedImage.load(base64Dec.toByteArray());
}
In the 'generatedImage', the image is shown, so I assume this routine works.
Now I want to decode the balanceSheetImage in Java. For this reason, I created following Java code. The value contains the string in the generatedImage, which I copy/pasted from the flex code.
import sun.misc.BASE64Decoder;
public static int createPdf() throws IOException{
String value= "t3j1fA31VBp0DAADAJfR3rOfp+4+0H/qO+b//89/6u23566+//mVmZmZmZmb2yl4N8dSFcAzj0t29Xp//x77HfCvc/f8yAAAAfEL8FPT5wdm6/OBrf9SnsgtzAAAA+CBhDgAAAMIcAAAAhLkwBwAAAGEOAAAAwlyYAwAAgDAHAAAAYQ4AAAAIcwAAADifusErBAAAADtyxxwAAACEOQAAAAhYQ4AAADCHAAAAIS5MAcAAABhDgAAwEXN99W29OT8doBzYQ4AAMDppBbCqxrOy4EeC3MAAAAuYY3T3HaUc2EOAADAqa0/Pp5DIMcfay8fPhfmAAAAXELutvoj7phvhfeWZVnMzMzMzMzMXtqrIZ66EM7hrrX3mAMAAMAA8VPQ5yfnPpUdAAAAEOYAAAAgzAEAAABhDgAAAMIcAAAAEOYAAAAgzAEAAABhDgAAAMIcAAAA+K5u8AoBAADAjtwxBwAAAGEOAAAAwlyYAwAAgDAHAAAAYS7MAQAAQJgDAABwUeW+2lbC+RzObwc4F+YAAACcTmohHKM4tZXwnE8/FuYAAABcQm5BfOsCeQ5fz+H5ddC5MAcAAOD0+rvnafr/H29fw31VBp0LcwAAAE5t7qI8h9/fpnF3xn90x3wrvLcsy2JmZmZmZmb20n4S5aW7S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Mc6O/65P068bI/GxDoCDyeGbAoy43oQ5e/KPC/Zwa98nkzBnpzBf42j2cvDmf9f3P+lTdQAc/5sC7Cn5Bwc7Wn9MTzSxhxr+O+Ya493fG/tQ8n9g885/1/fXVNEBIMy5djSJckbwHmD2uKZqN9cYe4a664t3h/mzO+Y6AIQ5F7u2/L//jPpHrDBHNPGn/zfM90ze+e/6373HXAeAMOci+k+adaeJva8zP5mBMOdP/m+Y64s9/l2/9ansOgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOBY/geGctMTogR6kgAAAABJRU5ErkJggg==";
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(value);
ByteArrayInputStream str = new ByteArrayInputStream(imgBytes);
BufferedImage bufImg = ImageIO.read(str);
File imgOutFile = new File("newLabel.png");
ImageIO.write(bufImg, "png", imgOutFile);
return -1;
}
Unfortunately, this does not work. In particular: BufferedImage is always null. Has anyone a clue why this happens?

Related

Converting EMF Image from MS Word to BufferedImage

I am writing a program that would read text from an image. I have success on JPG/PNG by getting the BufferedImage out of the XWPFPictureData.
public static BufferedImage convertPicDataToBuffImg(XWPFPictureData objPicData) {
byte[] bData = objPicData.getData();
BufferedImage objInBuffImg = ImageIO.read(new ByteArrayInputStream(bData));
...
}
However, when using the same code for EMF picture types, objInBuffImg becomes null. As I read, this is because the type is EMF.
To use the same methods I used, I wanted to convert XWPFPictureData with PictureType Document.PICTURE_TYPE_EMF to BufferedImage.
So basically what I wanted to do is something like the following:
public static BufferedImage convertPicDataToBuffImg(XWPFPictureData objPicData) {
BufferedIMage objInBuffImg = null;
if (objPicData.getPictureType() == Document.PICTURE_TYPE_EMF)) {
// Convert to BufferedImage
} else {
byte[] bData = objPicData.getData();
objInBuffImg = ImageIO.read(new ByteArrayInputStream(bData));
}
...
}
Have been searching and read about Batik but can't exactly figure out how to do what I want to do as stated above.
Any ideas?
Thank you very much in advance for your help!

Create image file from raw string data

for example, i have String like this:
data:image/png;base64,iVBORw0K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U2F66CEgzQXjQRUjjb8aX7tDaYFpwKkgAi0SSAUXaO0Pjkk/HUoKFQ9p0wm/hjcONC2B6W3B24KKv1ZLx0vzgfQoFsyHQJe3LQINHUEZrUNre6wO1aHLw+AvO5QOHdReLbE0/vSeedyhKBWUDh00XpoAAg2/EkIAqD0FlPYXqEDp3Pix/b8/FKUOIMem7fR6j8Yr0fvlYoEWK4JAw0dplOE6dLnrqH5JrCp4NcMFejPQ6h7RnTAUT/eTKkpYiidtH99D04C6bwvS+QX65W8sUMkVaKgAlcRwuLfuNL5Ah/fQKkC6Pi2JKXB6NEjxUTslKF1P7e17KE1YbRfoZwUFuuijQ4v/l5s6VocOOzQFYv9ZBcoldzY8R08VEGiq2Ob9Bbo5oDQ8gaaKbd5foJsDSsMTaKrY5v0FujmgNDyBpopt3l+gmwNKwxNoqtjm/QW6OaA0PIGmim3eX6CbA0rDE2iq2Ob9Bbo5oDS8H8eCMw7yCzx+AAAAAElFTkSuQmCC
i want to convert it back to an image file. here's what i've tried:
public void dumpFromRawData(String rawData, String wheretoPut) throws Exception{
byte[] imageByte = rawData.getBytes();
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageByte));
File file = new File(wheretoPut);
ImageIO.write(bufferedImage, "png", file);
}
where rawData is the example string above, and wheretoPut is the new image directory.. for example D:\\image.png
but when i run it, it gives me this:
Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
at javax.imageio.ImageIO.write(ImageIO.java:1520)
how could i resolve this? or is this not the correct way to convert it back to image?
The data of your image seems to be base64 encoded so you can not just convert this string into a byte array.
So you should use something like:
byte[] imageByte = Base64.decode(rawData, Base64.DEFAULT);
i finally found the solution. Thans #Chris623 for enlightening me :D
first, i need to separate the 'header' of the string file.. the data:image/png;base64, string, and then decode it to byte[]
String separator = ",";
String encoded = rawData.split(separator)[1];
byte[] decodedByte = Base64.getDecoder().decode(encoded.getBytes(StandardCharsets.UTF_8));
and then the rest of process is still the same.. the full method looks like this:
public void dumpFromRawData(String rawData, String wheretoPut, String fileName) throws Exception{
String separator = ",";
if(rawData.contains(separator)) {
// use this when the decoded string contains "," separator, like data:image/png;base64,
String encoded = rawData.split(separator)[1];
byte[] decodedByte = Base64.getDecoder().decode(encoded.getBytes(StandardCharsets.UTF_8));
// you can use this
// Path destinationFile = Paths.get(wheretoPut, fileName);
// Files.write(destinationFile, decodedByte);
// or this
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(decodedByte));
File file = new File(wheretoPut+fileName);
ImageIO.write(bufferedImage, "png", file);
System.out.println("File has been Written as " + wheretoPut + fileName);
} else {
System.out.println("i haven't think about it yet.");
}
}

Scan, detect and decode UPC code from an image

I am working on Android development where once I get byte array from Google Glass frame, I am trying to scan array using Zxing library and trying to detect 1d barcode(UPC code).
I have tried this code snippet.
BufferedImage image = ImageIO.read(game);
BufferedImageLuminanceSource bils = new BufferedImageLuminanceSource(image);
HybridBinarizer hb = new HybridBinarizer(bils);
BitMatrix bm = **hb.getBlackMatrix();**
MultiDetector detector = new MultiDetector(bm);
DetectorResult dResult = detector.detect();
if(dResult == null)
{
System.out.println("Image does not contain any barcode");
}
else
{
BitMatrix QRImageData = dResult.getBits();
Decoder decoder = new Decoder();
DecoderResult decoderResult = decoder.decode(QRImageData);
String QRString = decoderResult.getText();
System.out.println(QRString);
}
It works fine for QRcode, detects and decodes QR code well. But does not detect UPC code.
I also tried this code snippet,
InputStream barCodeInputStream = new FileInputStream(game);
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
BufferedImage image = ImageIO.read(game);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
int rowNumber = bitmap.getHeight()/2;
BitArray row = **bitmap.getBlackRow(0, null);**
Result theResult = rssExpandedReader.decodeRow(rowNumber, row, new Hashtable());
and in both I am getting "Exception in thread "main" com.google.zxing.NotFoundException".
Does anyone know how to fix this issue?
getBlackMatrix() -
Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or may not apply sharpening. Therefore, a row from this matrix may not be identical to one fetched using getBlackRow(), so don't mix and match between them.
getBlackRow()-
Converts one row of luminance data to 1 bit data. May actually do the conversion, or return cached data. Callers should assume this method is expensive and call it as seldom as possible. This method is intended for decoding 1D barcodes and may choose to apply sharpening.

How to send an image from a Java websocket server to use in an HTML5 canvas?

I have a WebSocket server implemented in Java. When a client connects I want to send an image over this connection for the client to use in a canvas element. I have come up with the following server code:
public void onOpen(Connection connection) {
try {
BufferedImage image = ImageIO.read(new File("image.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte[] byteArray = baos.toByteArray();
connection.sendMessage(byteArray, 0, byteArray.length);
} catch (Exception e ){
System.out.println("Error: "+e.getMessage());
}
}
The client-side Javascript looks like this:
onmessage : function(m) {
if (m.data) {
if (m.data instanceof Blob) {
var blob = m.data;
var bytes = new Uint8Array(blob);
var image = context.createImageData(canvas.width, canvas.height);
for (var i=0; i<bytes.length; i++) {
image.data[i] = bytes[i];
}
}
}
}
The connection works and the data is sent (blob.size has the correct value), but the image is not drawn onto the canvas. Firefox gives me the error message "TypeError: Value could not be converted to any of: HTMLImageElement, HTMLCanvasElement, HTMLVideoElement.".
I am aware of the fact that this using WebSockets is not the best way to send an image to the client. After sending the image the WebSocket is only used to send text messages.
What do I need to change for the image to be sent an applied to the canvas?
Resources used:
how to convert image to byte array in java?
Receive Blob in WebSocket and render as image in Canvas
Try converting the image to base64 before sending, for example:
function drawImage(imgString){
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var image = new Image();
image.src = imgString;
image.onload = function() {
ctx.drawImage(image, 0, 0);
};
}
Here's a link on how to convert the image to base64 in Java

How to set the value of a byte[] variable so that it can hold all an Image?

I have photos in the phone device. I want to get the byte[] value of a photo. I use this code to get it :
fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/", Connector.READ);
if (fcDir.exists())
{
Enumeration filelist = fcDir.list("*", false);
while (filelist.hasMoreElements())
{
String fileName = (String) filelist.nextElement();
fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/"+fileName, Connector.READ);
dis = fcFile.openDataInputStream();
byte[] rawImg = new byte[10240];
dis.readFully(rawImg);
Image tmpImg = Image.createImage(rawImg, 0, rawImg.length);
new Fimage(tmpImg).show();
}
}
The Fimage LWUIT Form is shown successfully and it has the Image tmpImg as its background image : not all the Form is occupyied by the Image but only 3/4 of the Form.
My problem is that I do not know what number to set exactly for the argument at the line byte[] rawImg = new byte[10240]; . I set the argument to 10240 , but it is not very clever. So how to set exactly this number ?
The FileConnection class has a method fileSize() -- call it, and use the result as the size for the array.

Categories