Results 1 to 3 of 3
  1. #1
    Mazharul is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default Regarding the display of histogram

    Hi all
    I have function for histogram calculation and display .Simply as a novice about JAVA2D I want to reuse this code with a stand alone application which shows picture and store pixel array in a .txt file plus histogram. Your help will be regarding accommodation of this function with this Stand alone program please.

    //Function: I have changed all the internal parameter of this function that need to be changed for inclusion in program next to this function


    private BufferedImage histogram() throws IOException {

    String path = "C:/srk.jpg";
    BufferedImage image = ImageIO.read(new File(path));
    int width = image.getWidth();
    int height= image.getHeight();
    WritableRaster raster = image.getRaster();
    int[][] data = new int[height][width];
    for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
    data[y][x] = raster.getSample(x, y, 0);
    }
    }

    int maxCount = 0, levelCount[] = new int[256];
    for (int row = 0; row < height; row++)
    for (int col = 0; col < width; col++)
    if (++levelCount[data[row][col]] >
    maxCount)
    maxCount = levelCount[data[row][col]];
    BufferedImage bufImage =
    new BufferedImage(256,128,
    BufferedImage.TYPE_BYTE_GRAY);
    Graphics gc = bufImage.getGraphics();
    gc.setColor(Color.white);
    gc.fillRect(0,0,256,128);
    gc.setColor(Color.black);
    for (int i = 0; i < 256; i++)
    gc.drawLine(i,127,i,128-
    (int)((double)levelCount[i]/maxCount*128));
    return bufImage;
    }






    // This code show a picture and store pixel array into a .txt file.Plz accommodate above function
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;

    public class BufferConversion {
    private JScrollPane getContent(BufferedImage image) {
    int[][] data = extractData(image);
    String path = "bufferConversion.txt";
    writeToFile(data, path);
    JLabel label = new JLabel(new ImageIcon(image), JLabel.CENTER);
    return new JScrollPane(label);
    }

    private int[][] extractData(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();
    System.out.printf("w = %d h = %d%n", w, h);
    WritableRaster raster = image.getRaster();
    int[][] data = new int[h][w];
    for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
    data[y][x] = raster.getSample(x, y, 0);
    }
    }
    return data;
    }

    private void writeToFile(int[][] data, String path) {
    int h = data.length;
    int w = data[0].length;
    int[] array = new int[h*w];
    for(int y = 0; y < h; y++) {
    for(int x = 0; x < w; x++) {
    int index = y*w + x;
    array[index] = data[y][x];
    }
    }
    try {
    File file = new File(path);
    BufferedWriter bw = new BufferedWriter(
    new OutputStreamWriter(
    new FileOutputStream(file)));

    for(int i = 0; i < array.length; i++) {
    String s = String.valueOf(array[i]) + " ";
    bw.write(s, 0, s.length());
    }
    bw.close();
    } catch(IOException e) {
    System.out.println("write error: " + e.getMessage());
    }
    }

    public static void main(String[] args) throws IOException {
    String path = "C:/HAND AA.jpg";
    BufferedImage image = ImageIO.read(new File(path));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new BufferConversion().getContent(image));
    f.setSize(500,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Where in the program is the call to histogram() supposed to go?

    Can you explain the logic flow for the program? What steps and in what order.

    Can you post your code with code tags? [CODE ] & [/CODE ] without the space.
    It makes it much easier to read.

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Have you looked at JFreeChart?
    Its handles tons of the stuff for you

Similar Threads

  1. Arrays and Histogram Help Needed
    By sebbybey in forum New To Java
    Replies: 4
    Last Post: 08-15-2008, 10:01 PM
  2. Arrays and Histogram Help Needed
    By sebbybey in forum New To Java
    Replies: 3
    Last Post: 08-15-2008, 10:00 PM
  3. Replies: 0
    Last Post: 06-28-2008, 10:26 PM
  4. How to create a Histogram
    By Devilsfutbol17 in forum New To Java
    Replies: 4
    Last Post: 06-04-2008, 10:22 PM
  5. GUI components not display
    By Eranga in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2008, 03:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •