i have question about sending and getting the encoded data for image. Firstly i have image as Base64 encoded type in String, this string has a value such as below :
...D/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZ...
Now if i decode again and if i use BitmapFactory to accomodate on imageview thats all right the image is ok.
byte[] bytes= stream.toByteArray();
imagestr=Base64.encodeBytes(bytes).toString();
//If i code below it is working
byte[] decode = Base64.decode(imagestr);
decoded = BitmapFactory.decodeByteArray(decode, 0, decode.length);
//If i send to the server and handle it in servlet file
String pic = request.getParameter("p");
byte[] servdec = Base64.decode(pic);
//and if i use the servdec to output a image file file is corrupted.
//I noticed the pic and imagestr are different
//imagestr = **...D/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZ...**
//pic = **...D/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko MzZ...**
//pic has no + sign.
I used replaceAll but it is only for this case. It may cause more prob. so is there any solution can you advice thank you for your answers...
Hi, this string is in pic which comes to this function, after this function servlet will handle this !pic has + sign in this function
public String uuidfaceid(String uuid,String faceid, String name,String pic){
URL url = null;
try {
url = new
URL("http://"+Constants.SERVER_NAME+Constants.SERVER_PORT+"/MeetInTouch/UF"+"?
uuid="+uuid+"&faceid="+faceid+"&name="+name+"&pic="+pic);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
URLConnection ucon = null;
try {
ucon = url.openConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
ucon.connect();
} catch (IOException e1) {
e1.printStackTrace();
}
The parameter that "p" that contains the Base64 encoded string has been "url decoded" at some point. All you have to do is encode it again before you try to decode the Base64:
String pic = request.getParameter("p");
pic = URLEncoder.encode(pic, "ISO-8859-1");
byte[] servdec = Base64.decode(pic);
Related
I am trying to convert an image(Web URL) to Base64 encoded string using Java. The below code works fine for the below URL
https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b
However, fails for most other image URLs
e.g. https://images.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
Seems like it fails in the openStream() step in the code.
try{
URI attURI = new URI(strURL);
importanttrace.addWarning(attURI.toASCIIString());
java.net.URL url = new java.net.URL(attURI.toASCIIString());
InputStream is = url.openStream(); //erroring
byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(is);
String res = Base64.getEncoder().encodeToString(bytes);
return res;
}
catch (Exception e) {
return "err";
}
Can someone help me understand what the issue may be? Thanks for your help!
EDIT: It is a FIREWALL issue.
I'm using cordova/phonegap to capture image with device (currently iphone), the capturing is working fine, I get the base64 encoded image as a string in a callback:
function handlePhotoSuccess(image){
document.getElementById("uploadItemForm:picture").src = "data:image/jpeg;base64," + image;
document.getElementById("uploadItemForm:imageBase64").value = image;
}
The imageBase64 element is a hidden h:inputText.
The image show correctly, but the saved value in the bean is not correct. It has like the image's 30% and the other part is gray.
The value in the backing bean is a String, I convert it to a byte array, then save it:
byte[] imageByte;
BASE64Decoder decoder = new BASE64Decoder();
try {
imageByte = decoder.decodeBuffer(imageString);
ByteArrayInputStream is = new ByteArrayInputStream(imageByte);
temp.setContent(is); //temp will be saved to database
images.put(currentAttribute.getId(), imageByte);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe I do something wrong?
Thank you in advance!
I am trying to convert Base64 encoded string (from image) [link to convertor]. Here is how I do this: First I convert the string, which is:
String str =
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQHBhMTEhQWFBUUGBUZGRYXFxgXGBQWFRQYGBQVFxgaISogGBomGxUXITEiJSkrLi4uFx8zODMsNygtLisBCgoKDgwMFw8PFywcFB4sLCwsNyssLDcrLCwsKzcsNzc3NzcsKywsKysrLCsrKywsNysrLCsrKyssKysrKysrK//AABEIALEBHQMBIgACEQEDEQH/xAAbAAEAAgMBAQAAAAAAAAAAAAAABAUCAwYBB//EADcQAAIBAwEFBQcEAgIDAQAAAAABAgMEERIFITFBURMiMnGhFBU0UmGRsXKBwe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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH//2Q==
like this:
byte[] imageString = str.getBytes("UTF-8");
and then I want to save the file on machine like this:
public boolean uploadImage(byte[] imageString, String name, String extension)
{
BufferedImage image = null;
String path = EVENT_FOLDER + name + "." + extension;
try
{
image = ImageIO.read(new ByteArrayInputStream(imageString)); //null returned here
ImageIO.write(image, extension, new File(path));
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
But I am getting null on this line: image = ImageIO.read(new ByteArrayInputStream(imageString));
What did I do wrong??
What did I do wrong?
You ignored the fact that the data is Base64-encoded, not UTF-8.
To convert the string to bytes to get the original data, you need to reverse that Base64 encoding, for example using this library.
byte[] imageData = Base64.decode(str);
String base64Code = dataInputStream.readUTF();
byte[] decodedString = null;
decodedString = Base64.decodeBase64(base64Code);
FileOutputStream imageOutFile = new FileOutputStream(
"E:/water-drop-after-convert.jpg");
imageOutFile.write(decodedString);
imageOutFile.close();
The problem is the data is transferred completely and if the data is in text format it is displayed correctly however when i am trying to decode image and write it on output file,it doesnt simply show up in photo viewer.
Any help would be highly appreciated
DataInputStream.readUTF may be the issue. This method assumes that the text was written to the file by DataOutputStream.writeUTF. If it is not so, and you are going to read a regular text, choose a different class, like BufferedReader or Scanner. Or Java 1.7's Files.readAllBytes.
Once I had to convert the Image into base 64 and sent that image as stream (encoding and decoding stuff here is the code)
To Convert a file into base64 :
String filePath = "E:\\water-drop-after-convert.jpg";
File bMap = new File(filePath);
byte[] bFile = new byte[(int) bMap.length()];
FileInputStream fileInputStream = null;
String imageFileBase64 = null;
try {
fileInputStream = new FileInputStream(bMap);
fileInputStream.read(bFile);
fileInputStream.close();
imageFileBase64 = Base64.encode(bFile);
}catch(Exception e){
e.printStackTrace();
}
then on service side I did thing like that to convert image in base 64 String back into file, so that i can display.
I had used this library on server side import sun.misc.BASE64Decoder;
//filePath is where you wana save image
String filePath = "E:\\water-drop-after-convert.jpg";
File imageFile = new File(filePath);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imageFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = null;
try {
decodedBytes = decoder.decodeBuffer(imageFileBase64);//taking input string i.e the image contents in base 64
} catch (IOException e1) {
e1.printStackTrace();
}
try {
fos.write(decodedBytes);
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
I am trying to send a encoded string to Solr and then decode it on retrieval. My encode looks like:
public static String compress(String inputString) {
try {
if (inputString == null || inputString.length() == 0) {
return null;
}
return new String(compress(inputString.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
private static byte[] compress(byte[] input) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(input);
gzip.close();
return out.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Then I send the to SOLR, and when I try to get it back (ignoring decoding for now because it fails here)
SolrDocument resultDoc = iter.next();
String content = (String) resultDoc.getFieldValue("source");
System.out.println(content);
If I send a string such as "Hello my name is Chris" the encoded will look like (ignoring what stack overflow changed);
ã�������ÛHÕ……W»≠T»KÃMU»,VpŒ( ,�ìùùG���
Yet what I get back from SOLR is
#31;ã#8;#0;#0;#0;#0;#0;#0;#0;ÛHÕ……W»≠T»KÃMU»,VpŒ( ,#6;#0;ìùùG#22;#0;#0;#0;
which will obviously make decoding fail. I have tried using the Jetty install and Tomcat both with the same issue.
See this entry from the example schema.xml file that comes with the Solr distribution.
<!--Binary data type. The data should be sent/retrieved in as Base64 encoded Strings -->
<fieldtype name="binary" class="solr.BinaryField"/>
Make sure that the field you are using to store your encoded value in the index is using the binary fieldType and that you are using base64 encoded strings.