utlis.HistogramPlot.java Source code

Java tutorial

Introduction

Here is the source code for utlis.HistogramPlot.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utlis;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author tarziz
 */
public class HistogramPlot {

    public JFreeChart plotGrayChart(int[] histogram) {

        XYSeries xysBrightnes = new XYSeries("Brightnes");

        for (int i = 0; i < histogram.length; i++) {

            xysBrightnes.add(i, histogram[i]);
        }

        XYSeriesCollection collection = new XYSeriesCollection();
        collection.addSeries(xysBrightnes);

        JFreeChart chart = ChartFactory.createXYLineChart("Histogram", "Color Value", "Pixel count", collection);
        try {
            ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500, 300);
        } catch (IOException ex) {
            Logger.getLogger(HistogramPlot.class.getName()).log(Level.SEVERE, null, ex);
        }
        return chart;
    }

    public JFreeChart plotColorChart(ArrayList<int[]> histograms) {

        XYSeries xysRed = new XYSeries("Red");
        XYSeries xysGreen = new XYSeries("Grean");
        XYSeries xysBlue = new XYSeries("blue");

        for (int i = 0; i < histograms.get(0).length; i++) {

            xysRed.add(i, histograms.get(0)[i]);
            xysBlue.add(i, histograms.get(2)[i]);
            xysGreen.add(i, histograms.get(1)[i]);
        }

        XYSeriesCollection collection = new XYSeriesCollection();
        collection.addSeries(xysRed);
        collection.addSeries(xysBlue);
        collection.addSeries(xysGreen);

        JFreeChart chart = ChartFactory.createXYLineChart("Histogram", "Color Value", "Pixel count", collection);
        try {
            ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500, 300);
        } catch (IOException ex) {
            Logger.getLogger(HistogramPlot.class.getName()).log(Level.SEVERE, null, ex);
        }
        return chart;
    }

}