你的位置:首页 > Java教程

[Java教程]卫星云图与雷达图生成图片


1.引用的Jar

 

image

 

2.

BitTransform.java

package com.test;public class BitTransform {  public static int getUShort(byte[] byteArray)  {    int value = 0;    value += (byteArray[0] & 0xff) | ((byteArray[1] & 0xff)<<8);    return value;  }    public static int getInt(byte[] byteArray)  {    int value = 0;    value += (byteArray[0] & 0xff) | ((byteArray[1] & 0xff)<<8) | ((byteArray[2] & 0xff)<<16) | ((byteArray[3] & 0xff)<<24);    return value;  }    public static float getFloat(byte[] byteArray)  {    int valueInt = 0;    valueInt += (byteArray[0] & 0xff) | ((byteArray[1] & 0xff)<<8) | ((byteArray[2] & 0xff)<<16) | ((byteArray[3] & 0xff)<<24);    float value = Float.intBitsToFloat(valueInt);        return value;  }}

 

DrawImage.java

package com.test;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Point;import java.awt.image.BufferedImage;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.imageio.ImageIO;import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class DrawImage {  private String dataPath = "";  private String imagePath = "";  //  private int imageWidth = 0;// 图片的宽度  private int imageHeight = 0;// 图片的高度  private String imageName = "";  //  private double minX = 0;  private double maxY = 0;  private double cellSize = 0;  //  private BufferedImage image;  //  public DrawImage(String dataPath, String imagePath) {    super();    this.imagePath = imagePath;    this.dataPath = dataPath;  }  // 解析数据  public int[][] getParseDataBzip2() {          int[][] dataFromParse = null;        try {      File fileBZip2 = new File(dataPath + "\\Z_OTHE_RADAMCR_201607070724.bin.bz2"); //"\\20160721060000.txt");      FileInputStream fileInputStream = new FileInputStream(fileBZip2);      BZip2CompressorInputStream gis = new BZip2CompressorInputStream(fileInputStream);       //zonName      int length = 12;      byte zonName[] = new byte[length];      gis.read(zonName, 0, length);      String zonNameStr = new String(zonName,"UTF-8");      System.out.println(zonNameStr);      //dataName      length = 38;      byte[] dataName = new byte[length];      gis.read(dataName, 0, length);      String dataNameStr = new String(dataName,"GB2312");      System.out.println(dataNameStr);      //year      length = 2;      byte[] year = new byte[length];      gis.read(year, 0, length);      int yearInt = BitTransform.getUShort(year);      System.out.println(yearInt);      //month      length = 2;      byte[] month = new byte[length];      gis.read(month, 0, length);      int monthInt = BitTransform.getUShort(month);      System.out.println(monthInt);      //day      length = 2;      byte[] day = new byte[length];      gis.read(day, 0, length);      int dayInt = BitTransform.getUShort(day);      System.out.println(dayInt);      //hour      length = 2;      byte[] hour = new byte[length];      gis.read(hour, 0, length);      int hourInt = BitTransform.getUShort(hour);      System.out.println(hourInt);      //minute      length = 2;      byte[] minute = new byte[length];      gis.read(minute, 0, length);      int minuteInt = BitTransform.getUShort(minute);      System.out.println(minuteInt);      //interval      length = 2;      byte[] interval = new byte[length];      gis.read(interval, 0, length);      int intervalInt = BitTransform.getUShort(interval);      System.out.println(intervalInt);      //      //      //XNumGrids      length = 2;      byte[] XNumGrids = new byte[length];      gis.read(XNumGrids, 0, length);      int XNumGridsInt = BitTransform.getUShort(XNumGrids);      System.out.println(XNumGridsInt);      //      //YNumGrids      length = 2;      byte[] YNumGrids = new byte[length];      gis.read(YNumGrids, 0, length);      int YNumGridsInt = BitTransform.getUShort(YNumGrids);      System.out.println(YNumGridsInt);      //      //ZNumGrids      length = 2;      byte[] ZNumGrids = new byte[length];      gis.read(ZNumGrids, 0, length);      int ZNumGridsInt = BitTransform.getUShort(ZNumGrids);      System.out.println(ZNumGridsInt);      //radarCount      length = 4;      byte[] radarCount = new byte[length];      gis.read(radarCount, 0, length);      //      // StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(24))      //StartLon      length = 4;      byte[] StartLon = new byte[length];      gis.read(StartLon, 0, length);      float StartLonFloat = BitTransform.getFloat(StartLon);      System.out.println(StartLonFloat);      //      //StartLat      length = 4;      byte[] StartLat = new byte[length];      gis.read(StartLat, 0, length);      float StartLatFloat = BitTransform.getFloat(StartLat);      System.out.println(StartLatFloat);      //CenterLon      length = 4;      byte[] CenterLon = new byte[length];      gis.read(CenterLon, 0, length);      float CenterLonFloat = BitTransform.getFloat(CenterLon);      System.out.println(CenterLonFloat);      //CenterLat      length = 4;      byte[] CenterLat = new byte[length];      gis.read(CenterLat, 0, length);      float CenterLatFloat = BitTransform.getFloat(CenterLat);      System.out.println(CenterLatFloat);      //XReso      length = 4;      byte[] XReso = new byte[length];      gis.read(XReso, 0, length);      float XResoFloat = BitTransform.getFloat(XReso);      System.out.println(XResoFloat);      //YReso      length = 4;      byte[] YReso = new byte[length];      gis.read(YReso, 0, length);      float YResoFloat = BitTransform.getFloat(YReso);      System.out.println(YResoFloat);      //ZhighGrids      length = 160;      byte[] ZhighGrids = new byte[length];      gis.read(ZhighGrids, 0, length);      //      //      imageWidth = XNumGridsInt; // NCOLS      imageHeight = YNumGridsInt;          dataFromParse = new int[imageHeight][imageWidth];      int count = imageWidth * imageHeight;      //      for(int i=0; i<imageHeight; i++)      {        for(int j=0; j<imageWidth; j++)        {          length = 2;          byte[] value = new byte[length];          gis.read(value, 0, length);          int valueInt = BitTransform.getUShort(value);          //          dataFromParse[i][j] = valueInt;        }      }            gis.close();       //      //      Calendar calendar = Calendar.getInstance();      calendar.set(yearInt, monthInt, dayInt, hourInt, minuteInt, 0);      Date date = calendar.getTime();      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");      imageName = simpleDateFormat.format(date);      minX = StartLonFloat;      maxY = StartLatFloat;            cellSize = XResoFloat;      //    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (NumberFormatException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    //    return dataFromParse;  }          public int[][] getParseData() {    imageName = "20160721060000";    imageWidth = 1200;    imageHeight = 900;    int[][] dataFromParse = null;            InputStreamReader read;    try {      //      File file = new File(dataPath + "\\20160721060000.txt");      read = new InputStreamReader(new FileInputStream(file));      BufferedReader bufferedReader = new BufferedReader(read);      String lineTxt = null;      // String[] a = bufferedReader.readLine().split("\\s+");      imageWidth = Integer.parseInt(bufferedReader.readLine().split("\\s+")[1]); // NCOLS      imageHeight = Integer.parseInt(bufferedReader.readLine().split("\\s+")[1]); // NROWS      dataFromParse = new int[imageHeight][imageWidth];      //      bufferedReader.readLine(); // XLLCENTER      bufferedReader.readLine(); // YLLCENTER      bufferedReader.readLine(); // CELLSIZE      bufferedReader.readLine(); // NODATA_VALUE      //      int index = 0;      while ((lineTxt = bufferedReader.readLine()) != null) {        String[] lineValue = lineTxt.split(" ");        for (int i = 0; i < lineValue.length; i++) {          dataFromParse[index][i] = Integer.parseInt(lineValue[i]);        }        index++;        //System.out.println(index);      }      read.close();    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (NumberFormatException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    return dataFromParse;  }  // 将解析后的数据转化为Color  public int[] getColorData(int[][] dataFromParse) {    int count = imageWidth * imageHeight;    int[] colorData = new int[count];    //    int index = 0;    for (int i = 0; i < imageHeight; i++) {      for (int j = 0; j < imageWidth; j++) {        Color color = new Color(dataFromParse[i][j], 0, 0, 255);        colorData[index++] = color.getRGB();      }    }    return colorData;  }  //  public void createImage(int[] colorData) {    image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);    image.getRaster().setDataElements(0, 0, imageWidth, imageHeight, colorData);  }  public void drawInfo() {    Graphics graphics = image.getGraphics();        drawBoundry(graphics);    drawTitle(graphics);    drawLegend(graphics);    graphics.dispose();  }  public void drawBoundry(Graphics graphics) {    //    try {      //      File file = new File(dataPath + "\\boudry.txt");      FileInputStream fileInputStream = new FileInputStream(file);      InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);      StringBuffer stringBuffer = new StringBuffer();      String lineTxt = null;      while ((lineTxt = bufferedReader.readLine()) != null) {        stringBuffer.append(lineTxt);      }      //      JSONObject jb = JSONObject.fromObject(stringBuffer.toString());      JSONArray featureArray = jb.getJSONArray("features");      //      graphics.setColor(Color.blue);      Point point = new Point();      for(int i=0; i<featureArray.size(); i++)      {        JSONArray ringArray = featureArray.getJSONObject(i).getJSONObject("geometry").getJSONArray("rings");        for(int j=0; j<ringArray.size(); j++)        {                    JSONArray ring = ringArray.getJSONArray(j);          int[] xPoints = new int[ring.size()];          int[] yPoints = new int[ring.size()];          for(int k=0; k<ring.size(); k++)          {              JSONArray coordinate = ring.getJSONArray(k);            //            double xx = coordinate.getDouble(0);            double yy = coordinate.getDouble(1);            //            coordinateTranform(xx, yy, point);            xPoints[k] = point.x;            yPoints[k] = point.y;          }          graphics.drawPolyline(xPoints, yPoints, xPoints.length);        }      }      inputStreamReader.close();    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (NumberFormatException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }  }  public void coordinateTranform(double x, double y, Point point)  {        int xResult = (int)((x - minX) / cellSize);    int yResult = (int)((maxY - y) / cellSize);        point.x = xResult;    point.y = yResult;  }    public void drawTitle(Graphics graphics) {    String title = imageName.substring(0, 4) + "年" + imageName.substring(4, 6) + "月" + imageName.substring(6, 8)        + "日" + imageName.substring(8, 10) + "时" + imageName.substring(10, 12) + "分";    graphics.setColor(Color.green);    Font font = new Font("宋体", Font.BOLD, 28);    graphics.setFont(font);    graphics.drawString("气象雷达图(" + title + ")", 400, 50);  }  public void drawLegend(Graphics graphics) {    graphics.setColor(Color.green);    graphics.fillRect(imageWidth - 120, imageHeight - 300, 120, 300);  }  public void saveImage() {    File file = new File(imagePath + "\\" + imageName + ".png");    try {      ImageIO.write(image, "png", file);    } catch (IOException e) {      e.printStackTrace();    }  }  public static void main(String[] args) throws FileNotFoundException, IOException {    DrawImage drawImage = new DrawImage("D:\\data", "D:\\test");    int[][] dataFromParse = drawImage.getParseDataBzip2(); // drawImage.getParseData();    int[] colorData = drawImage.getColorData(dataFromParse);    drawImage.createImage(colorData);    drawImage.drawInfo();    drawImage.saveImage();  }}

 

DrawImageFY2.java

package com.test;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Point;import java.awt.image.BufferedImage;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.imageio.ImageIO;import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class DrawImageFY2 {  private String dataPath = "";  private String imagePath = "";  //  private int imageWidth = 0;// 图片的宽度  private int imageHeight = 0;// 图片的高度  private String imageName = "";  //  private double minX = 0;  private double minY = 0;  private double cellSize = 0;  //  private BufferedImage image;  //  public DrawImageFY2(String dataPath, String imagePath) {    super();    this.imagePath = imagePath;    this.dataPath = dataPath;  }  // 解析数据  public int[][] getParseDataBzip2() {          int[][] dataFromParse = null;        try {      File file = new File(dataPath + "\\1607080900.000");      FileInputStream gis=new FileInputStream(file);      //zonName      int length = 51;      byte desc[] = new byte[length];      gis.read(desc, 0, length);      //dataName      //length = 5;      //byte[] dataName = new byte[length];      //gis.read(dataName, 0, length);      //year      length = 5;      byte[] year = new byte[length];      gis.read(year, 0, length);      System.out.println(5);      int yearInt = Integer.valueOf((new String(year)).trim());      System.out.println(yearInt);      //month      length = 3;      byte[] month = new byte[length];      gis.read(month, 0, length);      int monthInt = Integer.valueOf((new String(month)).trim());      System.out.println(monthInt);      //day      length = 3;      byte[] day = new byte[length];      gis.read(day, 0, length);      int dayInt = Integer.valueOf((new String(day,"GB2312")).trim());      System.out.println(dayInt);      //hour      length = 3;      byte[] hour = new byte[length];      gis.read(hour, 0, length);      int hourInt = Integer.valueOf((new String(hour,"GB2312")).trim());      System.out.println(hourInt);            //      //XNumGrids      length = 5;      byte[] XNumGrids = new byte[length];      gis.read(XNumGrids, 0, length);      int XNumGridsInt = Integer.valueOf((new String(XNumGrids,"GB2312")).trim());      System.out.println(XNumGridsInt);      //      //YNumGrids      length = 5;      byte[] YNumGrids = new byte[length];      gis.read(YNumGrids, 0, length);      int YNumGridsInt = Integer.valueOf((new String(YNumGrids)).trim());      System.out.println(YNumGridsInt);      //      // StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(24))      //StartLon      length = 8;      byte[] StartLon = new byte[length];      gis.read(StartLon, 0, length);      float StartLonFloat = Float.valueOf((new String(StartLon)).trim());      System.out.println(StartLonFloat);      //      //StartLat      length = 8;      byte[] StartLat = new byte[length];      gis.read(StartLat, 0, length);      float StartLatFloat = Float.valueOf((new String(StartLat)).trim());      System.out.println(StartLatFloat);            //project      length = 2;      byte[] project = new byte[length];      gis.read(project, 0, length);      int projectInt = Integer.valueOf((new String(project)).trim());      System.out.println(projectInt);      //reso      length = 5;      byte[] reso = new byte[length];      gis.read(reso, 0, length);      float resoFloat = Float.valueOf((new String(reso)).trim());      System.out.println(resoFloat);      //imageType      length = 2;      byte[] imageType = new byte[length];      gis.read(imageType, 0, length);      int imageTypeInt = Integer.valueOf((new String(imageType)).trim());      System.out.println(imageTypeInt);      //YReso      length = 12;      byte[] refTable = new byte[length];      gis.read(refTable, 0, length);      //CenterLon      length = 8;      byte[] CenterLon = new byte[length];      gis.read(CenterLon, 0, length);      float CenterLonFloat = Float.valueOf((new String(CenterLon)).trim());      System.out.println(CenterLonFloat);      //CenterLat      length = 8;      byte[] CenterLat = new byte[length];      gis.read(CenterLat, 0, length);      float CenterLatFloat = Float.valueOf((new String(CenterLat)).trim());      System.out.println(CenterLatFloat);      //      //      imageWidth = XNumGridsInt; // NCOLS      imageHeight = YNumGridsInt;          dataFromParse = new int[imageHeight][imageWidth];      int count = imageWidth * imageHeight;      //      for(int i=0; i<imageHeight; i++)      {        for(int j=0; j<imageWidth; j++)        {          length = 1;          byte[] value = new byte[length];          gis.read(value, 0, length);          int valueInt =( value[0] & 0xff);          //          System.out.println(valueInt);          dataFromParse[i][j] = valueInt;        }      }            gis.close();       //      //      Calendar calendar = Calendar.getInstance();      calendar.set(yearInt, monthInt, dayInt, hourInt, 0, 0);      Date date = calendar.getTime();      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");      imageName = simpleDateFormat.format(date);      minX = StartLonFloat;      minY = StartLatFloat;      cellSize = resoFloat;      //    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (NumberFormatException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    //    return dataFromParse;  }      // 将解析后的数据转化为Color  public int[] getColorData(int[][] dataFromParse) {    int count = imageWidth * imageHeight;    int[] colorData = new int[count];    //    int index = 0;    for (int i = 0; i < imageHeight; i++) {      for (int j = 0; j < imageWidth; j++) {        Color color = new Color(dataFromParse[i][j], 0, 0, 255);        colorData[index++] = color.getRGB();      }    }    return colorData;  }  //  public void createImage(int[] colorData) {    image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);    image.getRaster().setDataElements(0, 0, imageWidth, imageHeight, colorData);  }  public void drawInfo() {    Graphics graphics = image.getGraphics();        drawBoundry(graphics);    drawTitle(graphics);    drawLegend(graphics);    graphics.dispose();  }  public void drawBoundry(Graphics graphics) {    //    try {      //      File file = new File(dataPath + "\\boudryFY2.txt");      FileInputStream fileInputStream = new FileInputStream(file);      InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);      StringBuffer stringBuffer = new StringBuffer();      String lineTxt = null;      while ((lineTxt = bufferedReader.readLine()) != null) {        stringBuffer.append(lineTxt);      }      //      JSONObject jb = JSONObject.fromObject(stringBuffer.toString());      JSONArray featureArray = jb.getJSONArray("features");      //      graphics.setColor(Color.blue);      Point point = new Point();      for(int i=0; i<featureArray.size(); i++)      {        JSONArray ringArray = featureArray.getJSONObject(i).getJSONObject("geometry").getJSONArray("rings");        for(int j=ringArray.size()-1; j>-1; j--)        {                    JSONArray ring = ringArray.getJSONArray(j);          int[] xPoints = new int[ring.size()];          int[] yPoints = new int[ring.size()];          for(int k=0; k<ring.size(); k++)          {              JSONArray coordinate = ring.getJSONArray(k);            //            double xx = coordinate.getDouble(0);            double yy = coordinate.getDouble(1);            //            coordinateTranform(xx, yy, point);            xPoints[k] = point.x;            yPoints[k] = point.y;          }          graphics.drawPolyline(xPoints, yPoints, xPoints.length);        }      }      inputStreamReader.close();    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (NumberFormatException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }  }  public void coordinateTranform(double x, double y, Point point)  {        minX = -3317409.926100;  // 86.590000;    minY = 397484.254000;   // -0.640000;    cellSize = 12915.0;  // 7.742934572202865e-05;    int xResult = (int)((x - minX) / 12958.632523828124); // + 86.590000     int yResult = 512 - (int)((y - minY) / 12872.168183593749);    // + 0.640000    point.x = xResult;    point.y = yResult;  }    public void drawTitle(Graphics graphics) {    String title = imageName.substring(0, 4) + "年" + imageName.substring(4, 6) + "月" + imageName.substring(6, 8)        + "日" + imageName.substring(8, 10) + "时" + imageName.substring(10, 12) + "分";    graphics.setColor(Color.green);    Font font = new Font("宋体", Font.BOLD, 28);    graphics.setFont(font);    graphics.drawString("气象雷达图(" + title + ")", 100, 50);  }  public void drawLegend(Graphics graphics) {    graphics.setColor(Color.green);    //graphics.fillRect(imageWidth - 120, imageHeight - 300, 120, 300);  }  public void saveImage() {    File file = new File(imagePath + "\\" + imageName + ".png");    try {      ImageIO.write(image, "png", file);    } catch (IOException e) {      e.printStackTrace();    }  }  public static void main(String[] args) throws FileNotFoundException, IOException {    DrawImageFY2 drawImage = new DrawImageFY2("D:\\data", "D:\\test");    int[][] dataFromParse = drawImage.getParseDataBzip2(); // drawImage.getParseData();    int[] colorData = drawImage.getColorData(dataFromParse);    drawImage.createImage(colorData);    drawImage.drawInfo();    drawImage.saveImage();  }}