I want to convert an image and an audio file into a binary stream, process it and then reconstruct the image from the same binary stream in Java. How can I do that? Has anybody worked on this? Please help me out as soon as possible. Any hints or pseudocode will be highly appreciated.
This is how I tried to do it but it just creates an empty file while reconstructing the image.
For image to binary:-
File file = new File("E:\\image.jpg");
BufferedImage img = ImageIO.read(file);
// write image to byte array in-memory (jpg format)
ByteArrayOutputStream b = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", b);
byte[] jpgByteArray = b.toByteArray();
// convert it to a String with 0s and 1s
StringBuilder sb = new StringBuilder();
for (byte by : jpgByteArray) {
sb.append(Integer.toBinaryString(by & 0xFF));
For binary to image:-
byte[] original = obj.orig_seq.getBytes();
InputStream in = new ByteArrayInputStream(original);
BufferedImage img = ImageIO.read(in);
ImageIO.write(img, "jpg",
new File("E:\\mypic_new.jpg"));
To use Binary String you can use this
StringBuilder sb = new StringBuilder();
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream("YourImage.jpg"))) {
for (int i; (i = is.read()) != -1;) {
String temp = "0000000" + Integer.toBinaryString(i).toUpperCase();
if (temp.length() == 1) {
sb.append('0');
}
temp = temp.substring(temp.length() - 8);
sb.append(temp).append(' ');
}
}
System.out.print(sb);
and here is your desired output
0000000 11111111 00000111 00011111 00101111 01111111
you can also use Hexadecimal and convert your file into a simple string like this (suggestion)
StringBuilder sb = new StringBuilder();
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream("YourFile.txt"))) {
for (int i; (i = is.read()) != -1;) {
String temp = Integer.toHexString(i).toUpperCase();
if (temp.length() == 1) {
sb.append('0');
}
sb.append(temp).append(' ');
}
}
System.out.print(sb);
and the output would be something like this
00 FF 07 1F 2F 7F
Related
i use ionic.zip gzipstream compress and decompress byte[],
it can compress well,but when it decompressed from byte[] compressed it told
Bad state (invalid stored block lengths)
my codes bellow
Trace.WriteLine(s.Length);
var b = Encoding.UTF8.GetBytes(s);
Trace.WriteLine(b.Length);
byte[] b2;
var sw = new Stopwatch();
sw.Start();
using (var m = new MemoryStream())
{
var stream = new GZipStream(m, CompressionMode.Compress,true);
stream.Write(b,0,b.Length);
stream.Flush();
b2 = m.GetBuffer();
stream.Close();
sw.Stop();
Trace.WriteLine(sw.ElapsedMilliseconds);
Trace.WriteLine(b2.Length);
}
using (var m2 = new MemoryStream())
{
m2.Write(b2,0,b2.Length);
m2.Position = 0;
var stream = new GZipStream(m2,CompressionMode.Decompress,true);
var m3 = new MemoryStream();
var buffer = new byte[1024];
var n = 1;
while (n != 0)
{
n = stream.Read(buffer, 0, buffer.Length);
if (n > 0)
{
m3.Write(buffer,0,n);
}
}
var b3 = m3.GetBuffer();
Trace.WriteLine(b3);
}
the decompress code is what the document said.here
and i found no other docs ,
what should i do when decompress?
Ah~~~, it's so fool.
there are byte[] compress and decompress methods.
GZipStream.CompressBuffer() and GZipStream.UncompressBuffer()
In my Windows Phone7.1 App Iam loading a HTML file from local path in a WebBrowser. For this I
converted a PNG Image to base64 format using the below code and the problem is base 64 format of image path is not loading the image in the webbrowser.
Please help me where i made mistake?
string s = "data:image/jpg;base64,";
imgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NewUIChanges.Htmlfile.round1.png");
byte[] data = new byte[(int)imgStream.Length];
int offset = 0;
while (offset < data.Length)
{
int bytesRead = imgStream.Read(data, offset, data.Length - offset);
if (bytesRead <= 0)
{
throw new EndOfStreamException("Stream wasn't as long as it claimed");
}
offset += bytesRead;
}
base64 = Convert.ToBase64String(data);
Stream htmlStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NewUIChanges.Htmlfile.equity_built.html");
StreamReader reader = new StreamReader(htmlStream);
string htmlcontent = reader.ReadToEnd();
htmlcontent = htmlcontent.Replace("round1.png", s + base64);
wb.NavigateToString(htmlcontent);
If you have no error, that data contains your image, and round1.png exist in htmlcontent, then it's just probably a image type error, try this:
string s = "data:image/png;base64,";
how to get image data from a bitmap image ? i searched, but i cant find a solution
int height=bmp.getHeight();
int width=bmp.getWidth();
int[] rgbdata = new int[width*height];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
//Graphics g = new Graphics(bmp);
bmp.getARGB(rgbdata,0,width,0,0,width,height);
for (int i = 0; i < rgbdata.length ; i++) {
if (rgbdata[i] != -1)
{
dos.writeInt(rgbdata[i]);
dos.flush();
}
}
bos.flush();
Try this:
PNGEncoder encoder = new PNGEncoder(bitmap, true);
byte[] imageBytes = encoder.encode(true);
And to get EncodedImage from byte array:
EncodedImage fullImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
I'm having a really interesting problem to solve:
I'm getting a static google map image, with an URL like this.
I've tried several methods to get this information:
Fetching the "remote resource" as a ByteArrayOutputStream, storing the Image in the SD of the Simulator, an so on... but every freaking time I get an IlegalArgumentException.
I always get a 200 http response, and the correct MIME type ("image/png"), but either way: fetching the image and converting it to a Bitmap, or storing the image in the SD and reading it later; I get the same result... the file IS always corrupt.
I really belive its an encoding problem, or the reading method (similar to this one):
public static Bitmap downloadImage(InputStream inStream){
byte[] buffer = new byte[256];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (inStream.read(buffer) != -1){
baos.write(buffer);
}
baos.flush();
baos.close();
byte[] imageData = baos.toByteArray();
Bitmap bi = Bitmap.createBitmapFromBytes(imageData, 0, imageData.length, 1);
//Bitmap bi = Bitmap.createBitmapFromBytes(imageData, 0, -1, 1);
return bi;
}
The only thing that comes to mind is the imageData.lenght (in the response, the content length is: 6005 ), but I really can't figure this one out.
Any help is more than welcome...
try this way:
InputStream input = httpConn.openInputStream();
byte[] xmlBytes = new byte[256];
int len = 0;
int size = 0;
StringBuffer raw = new StringBuffer();
while (-1 != (len = input.read(xmlBytes)))
{
raw.append(new String(xmlBytes, 0, len));
size += len;
}
value = raw.toString();
byte[] dataArray = value.getBytes();
EncodedImage bitmap;
bitmap = EncodedImage.createEncodedImage(dataArray, 0,dataArray.length);
final Bitmap googleImage = bitmap.getBitmap();
Swati's answer is good. This same thing can be accomplished with many fewer lines of code:
InputStream input = httpConn.openInputStream();
byte[] dataArray = net.rim.device.api.io.IOUtilities.streamToBytes(input);
Bitmap googleImage = Bitmap.createBitmapFromBytes(dataArray, 0, -1, 1);
I use the following code to retrieve image from the phone or SDCard and I use that image in to my ListField. It gives the output but it takes very Long time to produce the screen. How to solve this problem ?? Can any one help me?? Thanks in advance!!!
String text = fileholder.getFileName();
try{
String path="file:///"+fileholder.getPath()+text;
//path=”file:///SDCard/BlackBerry/pictures/image.bmp”
InputStream inputStream = null;
//Get File Connection
FileConnection fileConnection = (FileConnection) Connector.open(path);
inputStream = fileConnection.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int j = 0;
while((j=inputStream.read()) != -1) {
baos.write(j);
}
byte data[] = baos.toByteArray();
inputStream.close();
fileConnection.close();
//Encode and Resize image
EncodedImage eImage = EncodedImage.createEncodedImage(data,0,data.length);
int scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()),
Fixed32.toFP(180));
int scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()),
Fixed32.toFP(180));
eImage=eImage.scaleImage32(scaleFactorX, scaleFactorY);
Bitmap bitmapImage = eImage.getBitmap();
graphics.drawBitmap(0, y+1, 40, 40,bitmapImage, 0, 0);
graphics.drawText(text, 25, y,0,width);
}
catch(Exception e){}
You should read files once (on App start or before screen open, maybe put a progress dialog there), put images in array and use this array in paint.