Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 386|回复: 0

[图像处理学习]JAVA程序生成直方图

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:55:40 | 显示全部楼层 |阅读模式
    1. import java.awt.Color;  
    2. import java.awt.Font;  
    3. import java.awt.FontMetrics;  
    4. import java.awt.Graphics;  
    5. import java.awt.image.BufferedImage;  
    6. import java.io.File;  
    7. import java.io.IOException;  
    8.   
    9. import javax.imageio.ImageIO;  
    10.   
    11. /**
    12. * 柱状图
    13. *
    14. * @author lazy_p
    15. * @date 2010-3-20
    16. */  
    17. public class PlaneHistogram {  
    18.     private final int histogramWidth = 15;// 柱形图的宽度  
    19.     private final int histogramPitch = 10;// 柱形图的间距  
    20.     private float scaling = 1f;// 缩放的比例  
    21.     private int maxStrWidth = 0; // 字符串需要的最大宽度  
    22.   
    23.     /**
    24.      *
    复制代码

    1.      *   参数b[i]和str[i]必须对应
    2.      *
    复制代码
    * * @param g * @param title * @param v * @param str * @param color * 可以为空 */ public BufferedImage paintPlaneHistogram(String title, int[] v, String[] str, Color[] color) { int width = str.length * histogramWidth+str.length*histogramPitch+50; int height = 255; scaling = calculateScale(v, height);//计算缩放比例 BufferedImage bufferImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = bufferImage.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); FontMetrics metrics = null; g.setFont(new Font(null, Font.BOLD, 18)); g.setColor(Color.RED); g.drawString(title, (bufferImage.getWidth() - g.getFontMetrics() .stringWidth(title)) >> 1, 30);// 画标题 g.setFont(new Font(null, Font.PLAIN, 12)); metrics = g.getFontMetrics(); g.setColor(Color.BLACK); g.drawLine(10, 0, 10, height - 15); // 画Y坐标 g.drawLine(10, height - 15, width, height - 15);// 画X坐标 int j = 0; int colorCount=color.length; for (int i = 0; i < v.length; ++i) { if (color != null){ g.setColor(color[j]);// 设置前景色 if(j+1< colorCount){ j++; }else{ j=0; } }else{ g.setColor(Color.RED); } int x = 20 + i * (histogramPitch + histogramWidth + (maxStrWidth >> 1));// 计算出X坐标 int y = height - 16 - (int) (v * scaling); // 计算出Y坐标 // 画占的比例 g.drawString(v + "", x - ((metrics.stringWidth(v + "") - histogramWidth) >> 1), y); // 画平面的柱状图 g.drawRect(x, y, histogramWidth, (int) (v * scaling)); g.fillRect(x, y, histogramWidth, (int) (v * scaling)); // 画每一项表示的东西 g.drawString(str, x - ((metrics.stringWidth(str) - histogramWidth) >> 1), height - 2); } return bufferImage; } /** * 计算缩放比例 * @param v * @param h 图片的高度 * @return */ public float calculateScale(int[] v , int h){ float scale = 1f; int max = Integer.MIN_VALUE; for(int i=0 , len=v.length ; i < len ;++i){ if(v>h && v>max){ max=v; } } if(max > h){ scale=((int)(h*1.0f/max*1000))*1.0f/1000; } return scale; } public static void main(String[] args) { PlaneHistogram planeHistogram = new PlaneHistogram(); BufferedImage image = planeHistogram.paintPlaneHistogram("颜色直方图", new int[]{100,200,300}, new String[]{"a" , "b" , "c"} , new Color[] {Color.RED, Color.GREEN, Color.BLACK, Color.BLUE }); File output = new File("c:/java/333.jpg"); try { ImageIO.write(image, "jpg", output); } catch (IOException e) { e.printStackTrace(); } } }  

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/28/235539875.rar
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 04:29 , Processed in 0.361999 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表