[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2238
  • Last Modified:

convert jpg file into byte file using java

I have a jpg image file, I want to convert it into byte file using java program, and what file extension i may use that. Because iReports requires, only byte file to show a static image in the report. such as a background image.
0
jayaprakashkg
Asked:
jayaprakashkg
  • 2
1 Solution
 
ProgrammingSmurfCommented:
not sure what you mean by Byte file?  You can read it into a byte array with something like this ...


package com.example.ExpertsExchange
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadJPG {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	// TODO Auto-generated method stub
 
	try {
		FileInputStream fi = new FileInputStream("C:\\image.jpg");
		byte[] imageAsBytes = new byte[fi.available()];
		fi.read(imageAsBytes);
			
	} catch (FileNotFoundException ex) {
		// error processingpackage uk.allianz.test.kmg.dirlist;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadJPG {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
		try {
			FileInputStream fi = new FileInputStream("C:\\image.jpg");
			byte[] imageAsBytes = new byte[fi.available()];
			fi.read(imageAsBytes);
			
		} catch (FileNotFoundException ex) {
			// error processing
		} catch (IOException ex) {
			// error pro
		}
	}
 
}	

Open in new window

0
 
ProgrammingSmurfCommented:
sorry ... i cut and paste twice by mistake ,,,, code snippet should be ,.,.,.
package uk.allianz.test.kmg.dirlist;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadJPG {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
		try {
			FileInputStream fi = new FileInputStream("C:\\image.jpg");
			byte[] imageAsBytes = new byte[fi.available()];
			fi.read(imageAsBytes);
			
		} catch (FileNotFoundException ex) {
			
		} catch (IOException ex) {
			
		}
	}
 
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now