L遷客

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

          用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)  編輯  收藏


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 曲靖市| 嘉鱼县| 瓮安县| 巴南区| 绍兴市| 滁州市| 赫章县| 五寨县| 廉江市| 高陵县| 乌鲁木齐市| 北碚区| 刚察县| 肥西县| 大邑县| 重庆市| 元谋县| 大同县| 准格尔旗| 昌图县| 杭锦后旗| 沙湾县| 阿坝县| 成武县| 逊克县| 九寨沟县| 光泽县| 沿河| 建瓯市| 吉林省| 长岛县| 洪雅县| 江达县| 杭州市| 鲁山县| 襄樊市| 和平区| 嘉义市| 手机| 彩票| 健康|