scala 读雷达数据文件,生成png
使用scala语言,从雷达文件中读取二维矩阵,并将矩阵保存成png
代码如下:
import scala.io.Source import java.io._ import java.awt.Image import java.awt.Toolkit import java.awt.image._ import javax.imageio._ import java.awt.Color
import scala.reflect.ClassTag object RadareTest { def main(arges:Array[String]){ //0~15对应的颜色值 val cormap = Map(0-> new Color(0,0,0).getRGB, 1->new Color(170,170,170).getRGB, 2->new Color(118,118,118).getRGB, 3->new Color(0,254,254).getRGB, 4->new Color(0,174,174).getRGB, 5->new Color(0,254,0).getRGB, 6->new Color(0,142,0).getRGB, 7->new Color(254,0,254).getRGB, 8->new Color(174,50,124).getRGB, 9->new Color(0,0,254).getRGB, 10->new Color(50,0,150).getRGB, 11->new Color(254,254,0).getRGB, 12->new Color(254,170,0).getRGB, 13->new Color(254,0,0).getRGB, 14->new Color(174,0,0).getRGB, 15->new Color(254,254,254).getRGB) //读文件,将文件内容转化为二维int数组 val file = Source.fromFile("E:/txt/txt_cr_Z9010_20130722/cr_int1_Z9010_20130722_001.txt") val arrInts = file.mkString.split("\\s+").filter(_.length > 0) .map(e=>cormap(e.toInt)).grouped(552).toArray //矩阵转置, 使二维数据符合在图片上的位置:从西向东,从南向北 val rc = transpose(arrInts).map(row=>row.reverse) val arrs = transpose(rc).flatten //关闭文件 file.close() //生成新图片 val width = 552 val height = 424 val imageNew = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) imageNew.setRGB(0,0,width,height,arrs,0,width) //设置RGB val outFile = new File("E:/Z9010_20130722_001.png") val bb = ImageIO.write(imageNew, "png", outFile) //写图片 println("write over!") } /* * 泛型函数 矩阵转置,行列互换 */ def transpose[T:ClassTag](xss: Array[Array[T]]): Array[Array[T]] ={ for (i <- Array.range(0, xss(0).length)) yield for (xs <- xss) yield xs(i) } }生成的图片如下: