Current location: Hot Scripts Forums » Programming Languages » Everything Java » create Image from String or byte Array


create Image from String or byte Array

Reply
  #1 (permalink)  
Old 05-09-07, 12:35 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
create Image from String or byte Array

Does anybody know of a way to create an image file, when all i have is the data that needs to be in the image file?
I'm currently working on a script to sort my mp3 files, and i want to store the image, which is written in one of the id3 tags, on my pc. So all i have is the data itself.

I tried using the FileWriter class, but then i get a messed up picture.
Here's waht i have so far (nothing much as you can see):
Java Code:
  1. public static void main(String[] args) {
  2.     try {
  3.         RandomAccessFile raf = new RandomAccessFile(
  4.                 new File(
  5.                         "E:\\Muziek - AlbumArts\\Anti-Flag - Live Fat, Die Young.jpg"),
  6.                 "r");
  7.         byte[] data = new byte[(int) raf.length()];
  8.         raf.read(data);
  9.         raf.close();
  10.         FileWriter fw = new FileWriter(new File("E:\\test.jpeg"));
  11.         fw.write(new String(data));
  12.         fw.close();
  13.     } catch (Exception e) {
  14.         e.printStackTrace();
  15.     }
  16. }
Is there a better way (i'm sure of it), and is there a way to store the image without errors?

Thanx in advance,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #2 (permalink)  
Old 05-09-07, 03:23 PM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
Use an outputstream:

Code:
        RandomAccessFile raf = new RandomAccessFile( new File("start.jpg"),"r");
        byte[] data = new byte[(int) raf.length()];
        raf.read(data);
        raf.close();
        OutputStream os = new FileOutputStream("result.jpg");
        os.write(data, 0, data.length);
        os.close();
You shouldn't throw away your old code, though. It creates a nice effect
__________________
my site

Last edited by King Coder; 05-09-07 at 03:26 PM.
Reply With Quote
  #3 (permalink)  
Old 05-09-07, 04:47 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Thanx man, again
Quote:
Originally Posted by King Coder View Post
You shouldn't throw away your old code, though. It creates a nice effect
I won't When i use the old code, i feel like Piet Mondriaan: squares and rectangles in different colors

here's an example of Piet Mondriaan: http://www.tamsquare.com/thumbnail/C..._Mondriaan.jpg

thanx again
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I need to pass this class Please Help! Negative6 Everything Java 1 05-03-07 06:55 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 05:42 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.