I am trying to print image on zebra printer(LP-2844-Z) using zpl language. In ZPL documentaion it says you have to convert image into ASCII HexaDecimal. Then by using GF command you can print image.
I tried below code to get image and convert it into hexa decimal
URL oracle = new URL(urlString);
URLConnection yc = oracle.openConnection();
BufferedImage bufferedImage = ImageIO.read(yc.getInputStream());
int rowsData = bufferedImage.getWidth()/8;
System.out.println(rowsData);
byte[] pixel = ((DataBufferByte)bufferedImage.getRaster().getDataBuffer()).getData();
System.out.println(pixel.length);
System.out.println(Hex.encodeHex(pixel, false));
And then I tried to print this data using zebra printer but it is not printing correct image. I tried another code to get image bytes and converted it into hexa decimal
URL oracle = new URL(urlString);
URLConnection yc = oracle.openConnection();
InputStream inputStream = yc.getInputStream();
byte[] imageData = IOUtils.toByteArray(inputStream);
System.out.println(Hex.encodeHex(pixel, false));
Still I am not able to print correct image. I follow following URL (http://labelary.com/viewer.html) and tried to see code when we upload image. What I found that after uploading image, the base64 generated by zebra viewer is totally different which I generated using above code. I go through several post on stackoverflow but still I am not able to solve out this problem.
I know where I am doing mistake but I don't know how to solve it. Actually I am not able to generate ASCII Hex Base64 code for given image. This is my thought.
Any help is appriciated.
Thanks,
Give this function a try:
private static String zplEncode(String graphicFileLocation) {
Path imagePath = Paths.get(URI.create("file://" + graphicFileLocation));
String returnResults = "";
try {
byte[] binaryData = Files.readAllBytes(imagePath);
for (byte b : binaryData)
{
String hexRep = String.format("{0:X}", b);
if (hexRep.length() == 1)
hexRep = "0" + hexRep;
returnResults += hexRep;
}
} catch (IOException ex) {
// Do something here
}
return returnResults;
}
I based it upon this answer from 2012.
Related
I'm trying to write some selenium/java test that checks 2FA configuration process. Thus I have to scan some QR code from a page in order to process it with zxing. The image format is Base64 and I'm struggling with decoding it to the byte array. The following code should convert base64 string to byte array, and then write it to the file.
Here is the code I wrote:
String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[#class='qr-code']")).getAttribute("src");
String base64Image = base64Source.split(",")[1];
byte[] decoded = Base64.getMimeDecoder().decode(base64Image);
try (OutputStream stream = new FileOutputStream("QR_CODE.png")){
stream.write(decoded);
}
This code compiles with no errors, but when I try to open generated png file I get only "Fatal error reading PNG image file: Decompression error in IDAT".
I know that base64 string is valid as I was able to convert it to the image using some online converter. Also, I checked the string with online validator and it said that this is a valid base64 MIME string.
Example of the base64 code below:
iVBORw0KGgoAAAANSUhEUgAAAeoAAAHqAQAAAADjFjCXAAAET0lEQVR4nO2dXYrrOgyAP50E5jGB%0AWUCX4uxgljScJd0dxEvpAgacx4KDzoPsxJ3hcqHppadUegiZxB9uQEjWjz2iHJD46wgNjjvuuOOO%0AO+64447fF5ciPbCUi8i4CnEEYBWZAJmWOnS63+yOvygeVFU1AfF0MV3TmU5lWnobob+lB+hUVVWv%0A8YOzO/6i+FLMl3yee2DI9kznzcx9pjK02MR7zu74a+H99wdx7LJAnyUoENJ7FpZ3lXi6yL1nd/w1%0A8R9aB6DxI6EsIxo/LqKQ/5/ZHX9NvGrdoMACwCp11dZlCQmVMK890Cks0OaVn/rbHX8wHkVEZARC%0A6lQ+zz0yATWkfVOCPVsthL3r7I6/GG62rjFfcezQeMooZLuD4SIwZPTa0j36xzv+pDiWBwkJ2NIi%0AJvOgqpo61XnI9e31uPmpv93xR+FUDcuoasZ0babk6wiaG8Xctc4yfK51jt8ku23D4oXdpA3F4Jlz%0AnSlWz+7c1jl+EJdpEYHhIjoDMgGq5x5gFQskPtMqsLypTEO2IX/Hj3f86fDqPrX4VXOpxa6pElJX%0AXli9YvO/buscv132qmrxmqlWWkMqvraEFECJOgb1dZ3jR6TRsNmebIu2Tbk21Su6NmS3dY4fkK0i%0AtgpB154wr6Iso9k1jeNXr1a5GPayWOc9J44fkbZvqfmzU8uc2LN0nUhJnjlx/JDUaGJP1e2BBJbD%0AI9SL5+scvwdeK2LLiIRznyWkUQhp7ZWly7C8F4MXpWtt4l1md/w18epShx91sN3+WeQaUmdRh8ew%0Ajt+pIgatXpWkSb3MUPWvZvNc6xy/Xbb+uq+eeLqIWgfdAqXTBASGhICAeWJdvdPJ8SPS5Ib3Smux%0AfzWugGrr2LtP3NY5frPsHtYammzlBm1tTBM0RbNmEfjU3+74o/A2XxdSUb1duZoti00M4bbO8WOy%0Ax7BNa1MJKaoSliE/1dG1zvGbpGSJm/r+FrTWAkXTbrf7Wrd1jt8uu4e9ustX6zrdfK3WdifXOsdv%0AlxrD0uThml46bdLCuyaCe1jHbxfL10mYFdvszzLaG2XpswCi8aOWwOKYtD0B4Km/3fFH4TVLvIxo%0AFNBy4gRQtv0DLAIMqca6/7zXpqen/nbHH4VfRRNbzcvWdQnM11odVrXuUfQY1vFDslXELqIsfdY4%0AfiEAGk+51zi9bS517Quw9G7rHD8iei0WNGyFryZeLXHtHle4rXP8VmlqE6W+muv2nGHbI7u72bRt%0AmXWtc/wgHmr6RCY6FTmVUxOt8SSc37YlXbnIdMfZHX8xvD3nZC/8A9TMnTabx8qL5PthHb8rvkez%0AZutYRSarza5iuvb75LbO8SPy84TYU0bCWcqpYXF6Uyx8HXJPnAAG35no+BH5flZnTZ+AMHz126md%0AaocTBwWBtb546m93/FH49xi2Pts26qheNRnvuyp8Xef4rSL632P+Xfy/1znuuOOOO+64447/Lfgf%0AFuoX02DU2vMAAAAASUVORK5CYII=
try this
String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[#class='qr-code']")).getAttribute("src");
String base64Image = base64Source.split(";")[1].split(",")[1]; //Try this
byte[] decoded = Base64.getMimeDecoder().decode(base64Image);
try (OutputStream stream = new FileOutputStream("QR_CODE.png")){
stream.write(decoded);
}
Okay, so i figured it out, so now it works.
The thing is when I run:
String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[#class='qr-code']")).getAttribute("src");
it adds a neweline (%0A) characters to the string so before decoding it to byte array I need to run qrCodeImage = qrCodeImage.replaceAll("%0A", ""); in order to remove them.
The Imgur API allows you to send a Base64 of an image to upload that image.
I have tested that this works when I send a Base64 that looks like this:
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQDAwQDAwQEAwQFBA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KD3Ig3y1b9zKVI3dER9rUPvDCJ3hBRxYU/ZGQOSVeeKaMBKb7u9cVHty1xh2VsPmUR1S36MRN2+Be0lwDkKOOyo+e4WS+JVGDwi4l0vajtP0scMxX/AHJ+3R0T/9k=
This one is really long and generated by the website http://www.base64-image.de/step-1.php
However, my application code generates a Base64 that looks like this
_9j_4AAQSkZJRgABAQEASABIAAD_2wBDAAYEBQ
It is shorter and I don't know why. Could someone please explain to me how to get the long version? Thankyou so much. Below is the code I am using to get the short version. I have obviously spent a long time Googling to figure this out and no one seems interested in this long thing.
String file1 = "C:\\Users\\J\\Desktop\\test5.jpg";
System.out.println("HERE");
FileInputStream imageInFile = new FileInputStream(file1);
byte imageData[] = new byte[(int) file1.length()];
imageInFile.read(imageData);
byte[] encodedBytes = Base64.encodeBase64(imageData);
System.out.println("encodedBytes " + new String(encodedBytes));
String convertedImageData = Base64.encodeBase64URLSafeString(imageData);
System.out.println(convertedImageData);
Below is the output of both of those:
HERE
encodedBytes /9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQ==
_9j_4AAQSkZJRgABAQEASABIAAD_2wBDAAYEBQ
Thanks for reading. I have tried MANY different things and come here now to ask.
imageInFile.read(imageData);
does not read the whole file into the array.
It reads some bytes length matching its internal buffer and returns as the result the number actually read.
You need to repeat the process in a loop till read no longer returns the data.
Check the javadoc.
Try these methods:
public static String imgToBase64String(final RenderedImage img, final String formatName) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
public static BufferedImage base64StringToImg(final String base64String) {
try {
return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64String)));
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
Taken from: Java BufferedImage to PNG format Base64 String
The years have passed...
Nowadays, I think the solution can be fount here: https://www.geeksforgeeks.org/url-encoding-decoding-using-base64-in-java/
So this worked for me:
Base64.getUrlEncoder().encodeToString(bytes);
To get the byte[] bytes (completely) filled as another topic...
I tested adding an html inline image when sending an email...
<img src="data:image/jpeg;base64,_9j_4AAQSkZ...
I am struggling with base64.
I would like to decode an image which comes though wysiwyg editor.
I build a source like below.(it is part of...)
InputStream is = request.getInputStream();
byte b[] = IOUtils.toByteArray(is);
String base64DataString = Base64.encodeBase64String(b);
String aaa = Base64.encodeBase64URLSafeString(b);
base64DataString = "data:image/" + ext + ";base64," + aaa;
if(aaa == "a"){}
if(is != null) {
is.close();
}
I got InputStream from request and make it as byte array.
size of byte[] b is exactly same with size of file I uploaded.
Then I combine a String to put src attribute of image tag.
After all of this when I look at in HTML. It has a broken image.
output looks like below :
<img src="data:image/jpeg;base64,_9j_4AAQSkZJRgABAQAAAQABAAD_2wCEAAkGBhQSERQUExQWFRUWGBgaGRgYFxwcGBwYFxwcGxgYGxoaHSYfHRwjGhUYHy8gIycpLCwsGB8xNTAqNSYrLCkBCQoKDgwOGg8PGi0kHyQsLCwsLCwuLCwsLywsLCwsLCwsLCwsLCwsLCksLCwsLCwsLCwsLCwsLCwsLCwsLCwsLP_AABEIAMIBAwMBIgACEQEDEQH_xAAcAAACAgMBAQAAAAAAAAAAAAAEBQMGAAECBwj_xABJEAABAwIDBAcDCAgDBwUAAAABAgMRACEEEjEFQVFhBhMicYGRoTKxwQcUQlJigtHwIzNykqKy0uEWc_EVNENEY5PCFyRTg6T_xAAbAQADAQEBAQEAAAAAAAAAAAACAwQBAAUGB__EADERAAIBAwMCBAQGAwEBAAAAAAABAgMRIRIxQRNRBCJhkXGhsfAFFFKB4fEyQtHBkv_aAAwDAQACEQMRAD8A3imEtCxPcEonzT8aru2GsOtehJt9Ig-QNAu4JeoQV8ogeakfGmOA7ABWyhs8S7J8gLeZqmThPl__ACUpSh8fiALwa0x83UUAi4OY-9UCjcF0cdVBUQeXVie_MVKNMBjmVHWD3qM92-abr24UtpCUrA49YNO6bd1ap6Vd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PlXO98o6yfIGvaDZ-kPAf2rQ2giJAUo8AlXviKKViUc_I_hWJxLfGuz2O0-oqdxKlf8JY8U_jQ-VZ0TFtSZPkLU8LyLXBrkrTJ7qG7GJIrbuyM11Eqjj8NwpbjOija57Nz4e6rsHERMihOvSJ9KJVJHaCfov0k6ppLGI7OVICHPoqyiBm-qqONjFjeKcDHpJsQQd4uPOkJdQoCbWtIgxxvuPupa_g0oOZtRQfsmAe8aHxFNjLuieVHOC9tYtMXrheLF71S2tpPDehXeCPcYqN7aOIVopCO4Sf4p91H5QenMuTmOFKsX0qZRILgJ4DtHyTNVksqcHbcUrkTbyECto2ekaeVC7INU3yxoemyCbIcV92PeRUa-mwH_AAnP4f6qXnBAGwFYvBgisduwxQXcJPTn_oufvI_qrKCGBHKsoL-gWmPc9SaSOFEpSKysr6NnykSdKRwrMg4CsrKEMzKOFclA4CsrKwwzIOArZbE6DdurdZWM1ETzYtYacKXLaF7DXhWVlYjXubS2OA8q7WkcKyspMimGwMtInStpTWVlCwjoisArKyhNR1lHCsyDgKysoGGjZaHAeVDPMpnQacKysoeQrgbzCfqjyFCqZTwHlWqyhaGxbIHGk8B5d1CuNi1hod1ZWUKQ1MFxDQgCBEcKExDKfqjyFZWVj3GJnDKYmOFRLF_zyrdZQ8lHARFvvD3VpSjfvHurKyuFAylmdTpWs5jU1lZRoxirFPqzntHzNbrKylmn_9k
" title="gaebul.jpeg">
What is the suspicious part of my code?
Thanks for sweet answer :D bb
You seem to be encoding two times without the real need, you should just do
byte[] encodeBase64 = Base64.encodeBase64(b);
String base64DataString = new String(encodeBase64 , "UTF-8");
and than just
src="data:image/jpeg;base64,[the value of your base64DataString]"
I am trying to update photo using Google apps engine.I have imageurl i convert it into byte array and then encode
it using base64.i got encoded string,now i m trying to update photodata using directory API Reference
https://developers.google.com/admin-sdk/directory/v1/reference/users/photos#resource
after update i got error invalid byteString.I face this problem from yesterday.So Let me know where i did wrong? Below is my code.
import com.google.appengine.repackaged.org.apache.commons.codec.binary.Base64;
above class used for Base64.
URL url = new URL(myImageUrl);
ByteArrayOutputStream bais = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
bais.write(byteChunk, 0, n);
}
System.out.println(byteChunk);
byte[] encoded = Base64.encodeBase64(byteChunk);
String ecodedString = new String(encoded, "UTF-8");
ecodedString = ecodedString.replace("/", "_");
ecodedString = ecodedString.replace("+", "-");
ecodedString = ecodedString.replace("=", "*");
System.out.println(ecodedString);
Padding could be the problem, try not replacing "=" with "*". See also:
Converting string to web-safe Base64 format
p.s. repackaged libs are discouraged in app engine; you may use DatatypeConverter.printBase64Binary() instead.
I'm using a jQuery plugin called wPaint to allow users to draw their own image. I send the resulting image as a string to the server as a string which begins with
data:image/png;base64,
I tried the two approaches below but with both the approaches i'm not able to store the image.
Approach 1
String imageData = parameterParser.getStringParameter("image", "");
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
FileOutputStream fileOutputStream = new FileOutputStream("/home/arvind/Desktop/test.png");
fileOutputStream.write(imgByteArray);
fileOutputStream.close();
In this case the file is written but doesn't show the image. However, when i remove the file extension i get the string that was sent to the server (i.e. whatever is in imageData).
Approach 2
String imageData = parameterParser.getStringParameter("image", "");
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
InputStream in = new ByteArrayInputStream(imgByteArray);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "png", new File("/home/arvind/Desktop/test.png"));
The BufferedImage bImageFromConvert is null so I get an exception (IllegalArgumentException) when the file is created.
The Base64 class is from the apache commons codec library and is version 1.2.
Is there something I'm doing wrong?
Initially I had sent the data to the server using the following code.
$.ajax({
url : '/campaign/holiImageUpload.action',
type : 'POST',
data : "image=" + $("#wPaint2").wPaint("image")
success :function(data){
}
});
Now i'm sending the data to the server using the following code
var imgData = $("#wPaint2").wPaint("image");
$.ajax({
url : '/campaign/holiImageUpload.action',
type : 'POST',
data : {image : imgData},
success :function(data){
}
});
In the server side this is the final code :
String imageData = parameterParser.getStringParameter("image", "");
try {
imageData = imageData.substring(22);
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
InputStream in = new ByteArrayInputStream(imgByteArray);
BufferedImage bufferedImage = ImageIO.read(in);
ImageIO.write(bufferedImage, "png", new File("/home/arvind/Desktop/test.png"));
catch(Exception ex){
ex.printStrackTrace();
}
It appears that you are trying to decode data:image/png'base64 along with your Base64 encoded data? You will want to remove that from the input string before you decode the Base64 data into image bytes.
Also, you don't want to decode the string as bytes... just as a string.