L遷客

          技術博客
          隨筆 - 1, 文章 - 12, 評論 - 1, 引用 - 0
          數據加載中……

          用java生成jpg圖片

           用java生成jpg圖片

          import com.sun.image.codec.jpeg.JPEGCodec;
          import com.sun.image.codec.jpeg.JPEGImageEncoder;

          import java.io.BufferedOutputStream;
          import java.io.FileOutputStream;
          import java.io.RandomAccessFile;
          import java.awt.image.BufferedImage;
          import java.awt.*;
          import java.util.Random;

          /**
           * Created by IntelliJ IDEA.
           * User: Administrator
           * Date: 2005-6-10
           * Time: 11:19:49
           * This class can create jpg picture.
           * To change this template use File | Settings | File Templates.
           */
          public class ChartGraphics {
              BufferedImage image;

              void createImage(String fileLocation) {
                  try {
                      FileOutputStream fos = new FileOutputStream(fileLocation);
                      BufferedOutputStream bos = new BufferedOutputStream(fos);
                      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
                      encoder.encode(image);
                      bos.close();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }

              public void graphicsGeneration(int h1, int h2, int h3, int h4, int h5) {

                  final int X = 10;
                  int imageWidth = 300;//圖片的寬度
                  int imageHeight = 300;//圖片的高度
                  int columnWidth = 30;//柱的寬度
                  int columnHeight = 200;//柱的最大高度

                  // ChartGraphics chartGraphics = new ChartGraphics();
                  image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
                  Graphics graphics = image.getGraphics();
                  graphics.setColor(Color.white);
                  graphics.fillRect(0, 0, imageWidth, imageHeight);
                  graphics.setColor(Color.red);
                  graphics.drawRect(X + 1 * columnWidth, columnHeight - h1, columnWidth, h1);
                  graphics.drawRect(X + 2 * columnWidth, columnHeight - h2, columnWidth, h2);
                  graphics.drawRect(X + 3 * columnWidth, columnHeight - h3, columnWidth, h3);
                  graphics.drawRect(X + 4 * columnWidth, columnHeight - h4, columnWidth, h4);
                  graphics.drawRect(X + 5 * columnWidth, columnHeight - h5, columnWidth, h5);
                  createImage("D://Temp//chart.jpg");
              }

              public static void main(String[] args) {
                  int[] height = {40,50,16,22,85};
                    ChartGraphics cg=new ChartGraphics();
                  try {
                      for (int i = 0; i < 5; i++) {
                          cg.graphicsGeneration(height[0],height[1],height[2],height[3],height[4]);
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }


              }
          }

          posted on 2013-02-24 16:42 L遷客 閱讀(753) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 郁南县| 延长县| 邵武市| 石城县| 宁武县| 安康市| 如皋市| 定远县| 濮阳县| 若尔盖县| 油尖旺区| 新巴尔虎左旗| 吉林市| 新沂市| 夏邑县| 松滋市| 云和县| 搜索| 富蕴县| 巴青县| 钟山县| 鄂尔多斯市| 葫芦岛市| 疏勒县| 贵溪市| 汉沽区| 文登市| 锡林郭勒盟| 景泰县| 齐河县| 沾益县| 神池县| 罗甸县| 高台县| 平顺县| 沅陵县| 龙州县| 龙里县| 潼南县| 琼海市| 井冈山市|