GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Jun-2007, 15:20
arety33 arety33 is offline
New Member
 
Join Date: Jun 2007
Posts: 1
arety33 is on a distinguished road

Importing images


I am new to java, can anyone tell me the command to display the image? Thanks!

JAVA Code:

import java.awt.*;
import hsa.Console;
import java.io.*;
import javax.imageio.*;
public class StringArrays
{
    static Console c;

    public static void importImage ()
    {
        Image image = null;
        try
        {
            File file = new File ("c.gif");
            image = ImageIO.read (file);
        }
        catch (IOException e)
        {
        }
    }



    public static void main (String[] args)
    {
        c = new Console ();
        importImage ();


    }
}

}
Last edited by arety33 : 06-Jun-2007 at 16:27. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 12-Jul-2007, 13:28
obduk obduk is offline
Junior Member
 
Join Date: May 2007
Posts: 54
obduk will become famous soon enough

Re: Importing images


Hi, I have not done java for a while, but here is a small image editing program I did for degree, it could read in file, then do basic edits like reverse. Have a look, its prob not brill code.

Code:
import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; import java.awt.event.*; import java.lang.Math; public class ImageEditor extends JFrame { JButton image_button, invert_button, slow_gamma_button, fast_gamma_button, convolve_button, convolution_button; JFrame frame; JLabel image_icon; BufferedImage image; File image_file; /* This function sets up the GUI and reads an image */ public ImageEditor() { image_file = new File("raytrace.jpg"); optionPane = new JOptionPane(); String input = (String) optionPane.showInputDialog("Enter A File"); optionPane.setVisible(true); try { image_file = new File(input); image = ImageIO.read(image_file); // Sets up the JFrame frame = new JFrame("Course Work"); frame.setBounds(200,100,680,600); frame.setResizable(false); Container contentPane = frame.getContentPane(); // Handler GUIEventHandler handler = new GUIEventHandler(); // Image Button image_button = new JButton("Open Image"); image_button.addActionListener(handler); // Invert Button invert_button = new JButton("Invert"); invert_button.addActionListener(handler); // Slow Gamma Button slow_gamma_button = new JButton("Slow Gamma"); slow_gamma_button.addActionListener(handler); // Fast Gamma Button fast_gamma_button = new JButton("Fast Gamma"); fast_gamma_button.addActionListener(handler); // Convolve Button convolve_button = new JButton("Convolve"); convolve_button.addActionListener(handler); // Convolution Button convolution_button = new JButton("Convolution"); convolution_button.addActionListener(handler); image_icon=new JLabel(new ImageIcon(image)); image_button.setBounds(2,2,110,25); contentPane.add(image_button); invert_button.setBounds(114,2,110,25); contentPane.add(invert_button); slow_gamma_button.setBounds(226,2,110,25); contentPane.add(slow_gamma_button); fast_gamma_button.setBounds(338,2,110,25); contentPane.add(fast_gamma_button); convolve_button.setBounds(450,2,110,25); contentPane.add(convolve_button); convolution_button.setBounds(562,2,110,25); contentPane.add(convolution_button); contentPane.add(image_icon); // Display the frame frame.setVisible(true); } catch(Exception e) { JOptionPane.showMessageDialog(null, "Error Reading File","Error", JOptionPane.WARNING_MESSAGE); } } /* This is the event handler for the application */ private class GUIEventHandler implements ActionListener { public void actionPerformed(ActionEvent event) { if (event.getSource()==image_button) { optionPane = new JOptionPane(); String input = (String) optionPane.showInputDialog("Enter A File"); optionPane.setVisible(true); try { image_file = new File(input); image = ImageIO.read(image_file); image_icon.setIcon(new ImageIcon(image)); } catch(Exception e) { JOptionPane.showMessageDialog(null, "Error Reading File","Error", JOptionPane.WARNING_MESSAGE); } } if (event.getSource()==invert_button) { // Call image processing function image=Invert(image); // Update image image_icon.setIcon(new ImageIcon(image)); } else if (event.getSource()==slow_gamma_button) { // Call image processing function image=SlowGamma(image); // Update image image_icon.setIcon(new ImageIcon(image)); } else if (event.getSource()==fast_gamma_button) { // Call image processing function image=FastGamma(image); // Update image image_icon.setIcon(new ImageIcon(image)); } else if (event.getSource()==convolve_button) { // Call image processing function image=BlueFade(image); // Update image image_icon.setIcon(new ImageIcon(image)); } if (event.getSource()==convolution_button) { // Call image processing function image=Convolution(image); // U[date image image_icon.setIcon(new ImageIcon(image)); } } } /* This function will return a pointer to an array of bytes which represent the image data in memory. Using such a pointer allows fast access to the image data for processing (rather than getting/setting individual pixels) */ public static byte[] GetImageData(BufferedImage image) { WritableRaster WR=image.getRaster(); DataBuffer DB=WR.getDataBuffer(); if (DB.getDataType() != DataBuffer.TYPE_BYTE) { throw new IllegalStateException("That's not of type byte"); } return ((DataBufferByte) DB).getData(); } /* This function shows how to carry out an operation on an image. It obtains the dimensions of the image, and then loops through the image carrying out the invert. It also shows a faster version commented out */ public BufferedImage Invert(BufferedImage image) { //Get image dimensions, and declare loop variables int w=image.getWidth(), h=image.getHeight(), j; //Obtain pointer to data for fast processing byte[] data = GetImageData(image); //Faster version - array index requires no calculation for (j=0; j<h*w*3; j++) { data[j] = (byte) (255-(data[j]&255)); } return image; } private JOptionPane optionPane; public BufferedImage SlowGamma(BufferedImage image) { //Get image dimensions, and declare loop variables int w=image.getWidth(), h=image.getHeight(), j; //Obtain pointer to data for fast processing byte[] data = GetImageData(image); double pixel; double gamma = 1; optionPane = new JOptionPane(); String input = (String) optionPane.showInputDialog("Enter The Gamma"); optionPane.setVisible(true); try { gamma = Double.parseDouble(input); for (j=0; j<h*w*3; j++) { pixel = (double) (data[j] & 255) / 255; data[j] = (byte) (Math.pow(pixel, gamma) * 255); } } catch(Exception e) { JOptionPane.showMessageDialog(null, "No Value Entered","Error", JOptionPane.WARNING_MESSAGE); } return image; } public BufferedImage FastGamma(BufferedImage image) { //Get image dimensions, and declare loop variables int w=image.getWidth(), h=image.getHeight(), i, j, c; //Obtain pointer to data for fast processing byte[] data = GetImageData(image); int pixel; double gamma = 1; double loop = 0; double corrections [] = new double[256]; optionPane = new JOptionPane(); String input = (String) optionPane.showInputDialog("Enter The Gamma"); optionPane.setVisible(true); try { gamma = Double.parseDouble(input); for (j=0; j<256; j++) { corrections[j] = (Math.pow((loop / 255), gamma) *255); loop ++; } for (j=0; j<h*w*3; j++) { pixel = (data[j] & 255); data[j] = (byte) corrections[pixel]; } } catch(Exception e) { JOptionPane.showMessageDialog(null, "No Value Entered","Error", JOptionPane.WARNING_MESSAGE); } return image; } public BufferedImage BlueFade(BufferedImage image) { //Get image dimensions, and declare loop variables int w=image.getWidth(), h=image.getHeight(), i, j, c; //Obtain pointer to data for fast processing byte[] data = GetImageData(image); int int_image[][][]; double t; int_image = new int[h][w][3]; // Copy byte data to new image taking care to treat bytes as unsigned for (j=0; j<h; j++) { for (i=0; i<w; i++) { for (c=0; c<3; c++) { int_image[j][i][c]=data[c+3*i+3*j*w]&255; } // colour loop } // column loop } // row loop // Now carry out processing on this different data typed image (e.g. convolution or "bluefade" for (j=0; j<h; j++) { for (i=0; i<w; i++) { int_image[j][i][0]=255*j/h; //BLUE int_image[j][i][1]=0; //GREEN int_image[j][i][2]=0; //RED } // column loop } // row loop // Now copy the processed image back to the original for (j=0; j<h; j++) { for (i=0; i<w; i++) { for (c=0; c<3; c++) { data[c+3*i+3*j*w]=(byte) int_image[j][i][c]; } // colour loop } // column loop } // row loop return image; } public BufferedImage Convolution(BufferedImage image) { //Get image dimensions, and declare loop variables int w=image.getWidth(), h=image.getHeight(), i, j, c, x, y; //Obtain pointer to data for fast processing byte[] data = GetImageData(image); int int_image[][][]; int_image = new int[h][w][3]; int min_pixel = 0; int max_pixel = 0; int box_width =0; int box_height =0; int box [] [] = new int [0][0]; Object[] possibleValues = { "Box Filter (3x3: All 1's)", "Gaussian Filter", "High Pass Filter", "6x4 Filter (All 1's)", "Custom Filter" }; Object selectedValue = JOptionPane.showInputDialog(null, "Select A Filter", "Convolution", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]); String option = (String) selectedValue; if(option.equals("Box Filter (3x3: All 1's)")) { box_width = 3; box_height = 3; box = new int [box_width][box_height]; for(x=0; x<3; x++) { for(y=0; y<3; y++) { box[x][y] = 1; } } } if(option.equals("Gaussian Filter")) { box_width = 3; box_height = 3; box = new int [box_width][box_height]; box[0][0] = 1; box[0][1] = 3; box[0][2] = 1; box[1][0] = 3; box[1][1] = 9; box[1][2] = 3; box[2][0] = 1; box[2][1] = 3; box[2][2] = 1; } if(option.equals("High Pass Filter")) { box_width = 3; box_height = 3; box = new int [box_width][box_height]; box[0][0] = -1; box[0][1] = -1; box[0][2] = -1; box[1][0] = -1; box[1][1] = 8; box[1][2] = -1; box[2][0] = -1; box[2][1] = -1; box[2][2] = -1; } if(option.equals("6x4 Filter (All 1's)")) { box_width = 6; box_height = 4; box = new int [box_width][box_height]; for(x=0; x<6; x++) { for(y=0; y<4; y++) { box[x][y] = 1; } } } if(option.equals("Custom Filter")) { optionPane = new JOptionPane(); String input1 = (String) optionPane.showInputDialog("Enter The Box Width"); String input2 = (String) optionPane.showInputDialog("Enter The Box Height"); optionPane.setVisible(true); box_width = Integer.parseInt(input1) ; box_height = Integer.parseInt(input2) ; box = new int [box_width] [box_height]; for(x=0; x < box_width; x++) { for(y=0; y<box_height; y++) { optionPane = new JOptionPane(); int input3 = Integer.parseInt(optionPane.showInputDialog("Enter Point " + x + " " + y)); optionPane.setVisible(true); box[x][y] = input3; } } } int box_left = (box_width-1)/2; int box_right = (box_width/2); int box_top = (box_height-1)/2; int box_bottom = (box_height/2); for (j=box_left; j<h-box_right; j++) { for (i=box_top; i<w-box_bottom; i++) { for (c=0; c<3; c++) { for (x=-box_left; x<=box_right; x++) { for (y=-box_top; y<=box_bottom; y++) { int_image[j][i][c] += (data[c+3*(i+x)+3*(j+y)*w]&255) * box[x+box_left][y+box_top]; } } if(int_image[j][i][c] > max_pixel) { max_pixel = int_image[j][i][c]; } if(min_pixel==0) { min_pixel = int_image[j][i][c]; } if(int_image[j][i][c] < min_pixel) { min_pixel = int_image[j][i][c]; } } } // column loop } // row loop for (j=0; j<h; j++) { for (i=box_top; i<w-box_bottom; i++) { for (c=0; c<3; c++) { int_image[j][i][c] = (((int_image[j][i][c] - min_pixel) * 255) / (max_pixel - min_pixel)); } } } // Now copy the processed image back to the original for (j=0; j<h; j++) { for (i=0; i<w; i++) { for (c=0; c<3; c++) { data[c+3*i+3*j*w]=(byte) int_image[j][i][c]; } // colour loop } // column loop } // row loop return image; } public static void main(String[] args) { ImageEditor example = new ImageEditor(); example.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

Owen
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 1) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading several images simultaneously yashin MS Visual C++ / MFC Forum 0 08-Apr-2007 13:03
Question regarding the use of images conus AdSense Forum 3 28-Dec-2006 07:38
Scaling Images Richardknox MySQL / PHP Forum 2 01-Dec-2006 10:25
Grab multiple images and display on a web page Saru .NET Forum 1 10-Mar-2006 15:35
Why are my images distorted? rhino1616 Graphics Forum 0 27-Jun-2003 09:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:39.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.