That is not what i am looking for
Main Topics
Browse All TopicsHello,
I need to convert an image into a byte array. I have noticed that there are different types of "byte arrays", I need the type that I can drop straight into a file and hit save as...
If you right click on a .jpg (or any other image) and open in a text editor... that's the kind of byte array I need.
Thanks in advance
Toby.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If you are only looking to modify a jpeg and then save it, then the BufferedImage class could make things a bit easier for you. There's some example code using BufferedImage here:
http://forum.java.sun.com/
May I ask what you are doing that requires a byte array?
If you really do need to read an image as a byte array, simply open it the same as you would open a text file to read.
http://java.sun.com/docs/b
1. Open the jpeg using a FileInputStream
2. Read it.
3. Do whatever you need to do to it.
4. Write it back using a FileOutputStream.
5. Close both streams when you are done.
heres an example
http://www.objects.com.au/
Ok..
I rad the byte array using:
File f = new File("C:/wasapps/temp/myIm
FileInputStream fin = new FileInputStream(f);
byte[] buffer = new byte[(int)f.length()];
fin.read(buffer);
fin.close();
I bas64 encode and pass the data to action script 3.
AS3 then tries to draw the byte array into bitmapData. , but the image is corrupted. It just draws funny colored dots, not the image that the user selected.
Why is the byte array damaged? Do i need a different type of byte array?
Everything else seems OK. When I Base64 Decode in AS3 the bytearrays are the same length, so im guessing nothing has been damaged.
I think that the byte array contents may be completely wrong....
I need to basically re-produce the following in Java:
var pixels : ByteArray = new ByteArray();
for( var i:uint=0; i<bd.width ; i++ )
{
for( var j:uint=0; j<bd.height; j++ )
{
pixels.writeUnsignedInt( bd.getPixel( i, j ) );
}
}
the getPixel() method returns an integer that represents an RGB pixel value from a BitmapData object at a specific point (x, y). The getPixel() method returns an unmultiplied pixel value. No alpha information is returned.
All pixels in a BitmapData object are stored as premultiplied color values. A premultiplied image pixel has the red, green, and blue color channel values already multiplied by the alpha data. For example, if the alpha value is 0, the values for the RGB channels are also 0, independent of their unmultiplied values.
I need to reproduce the same Byte Array as the AS3 above.
I hope that all made sense!
> When I Base64 Decode in AS3 the bytearrays are the same length
that doesn't sound right, I base64 should produce a string and i wouldn't expect it to be same length as array
> I think that the byte array contents may be completely wrong....
Are you sure its not the file
If you used the code from the link above the byte array would be identical to the file contents
tobydeh,
This question has been classified as abandoned. I will make a recommendation to the moderators on its resolution in approximately four days. I would appreciate any comments by the experts that would help me in making a recommendation.
It is assumed that any participant not responding to this request is no longer interested in its final disposition.
If the asker does not know how to close the question, the options are here:
http://www.experts-exchang
girionis
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: riaancorneliusPosted on 2007-07-03 at 11:48:39ID: 19413737
This class should do it (It asks for an input file - It will convert any type of file):
de(JFileCh ooser.FILE S_ONLY); l); ANCEL_OPTI ON) return; eOpenedFil e.getName( ).equals(" "))){ ile.getPat h()); (false); File.getPa th() + ".j"); ); nedFileLen gth); ).length() ;i++) out.write(nameOfArray.getT ext().char At(i));
ine.getTex t()); n e){maxColomnCount=20;} xColomnCou nt);
){ leReadArra y[i]).char At(0)); leReadArra y[i]).char At(1)); leReadArra y[i]).char At(2)); leReadArra y[i]).char At(3)); dsExceptio n e){} (i+1)*100/ openedFile Length)+"% "); ed(true);
log(null," The file "+theOpenedFile.getPath()+ " successfully converted.","Conversion Completted",JOptionPane.IN FORMATION_ MESSAGE); (true);
BorderLayo ut.CENTER) ; Layout.SOU TH); ); e); ray"); ; ze(new Dimension(600,30)); istener( t e){ ( t e){ ( t e){ /softcolle ction.syte s.net/java prog"); log(null, SSAGE); r( t e){ log(null," Converter of files into the Java array format v1.08","About",JOptionPane .INFORMATI ON_MESSAGE ); le);
Operation( WindowCons tants.EXIT _ON_CLOSE) ;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class JConverter extends JFrame{
private Container container = getContentPane();
private JProgressBar progressBar = new JProgressBar();
private JFileChooser chooser = chooser = new JFileChooser();
private JMenuBar bar = new JMenuBar();
private JMenu menuFile = new JMenu("File");
private JMenuItem menuConvertFile = new JMenuItem("Convert File");
private JMenuItem menuExit = new JMenuItem("Exit");
private JMenu menuHelp = new JMenu("Help");
private JMenuItem menuSite = new JMenuItem("Visit web site");
private JMenuItem menuAbout = new JMenuItem("About");
private JPanel panel = new JPanel();
private JLabel labelNameOfArray = new JLabel("Array name");
private JTextField nameOfArray = new JTextField(10);
private JLabel labelBytesPerLine = new JLabel("How many bytes per line");
private JTextField bytesPerLine = new JTextField(3);
private class PerformFile extends Thread{
public void run(){
File theOpenedFile=null;
FileInputStream in;
FileOutputStream out;
int resultOfChoice;
int readFromFileResult;
int maxColomnCount;
byte fileReadArray[];
int openedFileLength=0;
chooser.setFileSelectionMo
resultOfChoice = chooser.showOpenDialog(nul
theOpenedFile = chooser.getSelectedFile();
if (resultOfChoice==chooser.C
if (!(theOpenedFile==null||th
try{
in = new FileInputStream(theOpenedF
menuConvertFile.setEnabled
out = new FileOutputStream(theOpened
openedFileLength = (int)theOpenedFile.length(
progressBar.setMaximum(ope
out.write(' '); out.write(' ');
out.write('b'); out.write('y'); out.write('t'); out.write('e'); out.write(' ');
for (int i=0;i<nameOfArray.getText(
out.write('['); out.write(']'); out.write('='); out.write('{');
out.write(13); out.write(10); out.write(' '); out.write(' '); out.write(' '); out.write(' ');
fileReadArray = new byte[openedFileLength];
readFromFileResult = in.read(fileReadArray);
try{
maxColomnCount = Integer.parseInt(bytesPerL
if (maxColomnCount<10) maxColomnCount=10;
if (maxColomnCount>1000) maxColomnCount=1000;
}
catch(NumberFormatExceptio
bytesPerLine.setText(""+ma
for (int i=0;i<openedFileLength;i++
if (i>0) out.write(',');
if (i % maxColomnCount == (maxColomnCount-1)){
out.write(13); out.write(10); out.write(' '); out.write(' '); out.write(' '); out.write(' ');
}
try{
out.write(Byte.toString(fi
out.write(Byte.toString(fi
out.write(Byte.toString(fi
out.write(Byte.toString(fi
} catch(StringIndexOutOfBoun
progressBar.setValue(i+1);
progressBar.setString(""+(
progressBar.setStringPaint
}
out.write(13); out.write(10); out.write(' '); out.write(' ');
out.write('}'); out.write(';');
out.flush();
JOptionPane.showMessageDia
in.close();
out.close();
}
catch(IOException e){
System.out.println("Wrong Input or Output");
}
}
menuConvertFile.setEnabled
}
}
public JConverter(){
super("Converter of files into the Java array format (Example: byte newArray[]={...})");
container.add(progressBar,
container.add(panel,Border
panel.add(labelNameOfArray
panel.add(nameOfArray);
panel.add(labelBytesPerLin
panel.add(bytesPerLine);
nameOfArray.setText("newAr
bytesPerLine.setText("20")
progressBar.setPreferredSi
menuConvertFile.addActionL
new ActionListener(){
public void actionPerformed(ActionEven
PerformFile performFile = new PerformFile();
performFile.start();
}
}
);
menuExit.addActionListener
new ActionListener(){
public void actionPerformed(ActionEven
System.exit(0);
}
}
);
menuSite.addActionListener
new ActionListener(){
public void actionPerformed(ActionEven
JOptionPane jOptionPane = new JOptionPane();
JTextField jTextField = new JTextField();
jTextField.setText("http:/
jOptionPane.showMessageDia
jTextField,
"Please visit web site!",
JOptionPane.INFORMATION_ME
}
}
);
menuAbout.addActionListene
new ActionListener(){
public void actionPerformed(ActionEven
JOptionPane.showMessageDia
}
}
);
bar.add(menuFile);
menuFile.setMnemonic('F');
menuFile.add(menuConvertFi
menuFile.add(menuExit);
bar.add(menuHelp);
menuHelp.setMnemonic('H');
menuHelp.add(menuSite);
menuHelp.add(menuAbout);
setJMenuBar(bar);
pack();
setResizable(false);
show();
}
public static void main(String args[]){
JConverter jConverter=new JConverter();
jConverter.setDefaultClose
}
}