如何从存储在txt文件中的RGB数据创建BMP文件?

3
我需要从两个txt文件中创建一个bmp图像。第一个文件是一个mxn数组:

* * * * * * * *
m n
c11 c21 .. cm1
...
c1n c2n .. cmn
* * * * * * * *

* * * * * * * *
6 5
.7 .7 .6 1.0 1.2 .1
.9 .3 .7 1.1 .7 .2
1 1.1 1.2 1.3 1.7 .6
.5 .6 .5 .4 .9 .1101
2 .1 .1 .1 2.1 1.1
* * * * * * * *

第二个txt文件是一个颜色比例尺,如下所示:

* * * * * * * *
min1 max1 r1 g1 b1
min2 max2 r2 g2 b2
...
minx maxx rx gx bx
* * * * * * * *

* * * * * * * *
0 .5 255 128 64
.5 .75 128 255 32
.75 1.25 64 64 225
01.50 5 128 128 0
* * * * * * * *

因此,我需要从这两个文件中读取数据。我曾尝试使用StringTokenizer类从第一个txt文件中创建一个数组,但我完全迷失了方向。请问有人可以帮助我吗?


第二个文件中的最小/最大值是什么?第一个文件中数值的范围是多少?第一个文件中的数值应该如何映射到第二个文件或生成的图像中? - hhaamu
3个回答

4
如果颜色范围是连续的(你的示例缺少1.25-1.5),并且保证覆盖矩阵文件中所有可能的值,我会首先构建一个TreeMap<Double, java.awt.Color>,使用颜色文件中的最大值作为映射键。然后,可以使用TreeMap#ceilingEntry(K)方法获取任何矩阵值的颜色。例如,如果用你的测试数据正确填充了它,ceilingEntry(0.2).getValue()将返回Color(255,128,64)。
不必将矩阵文件读入数组中,你可以更容易地直接使用java.awt.BufferedImage进行绘制,然后使用javax.imageio.ImageIO将缓冲图像写入BMP文件。

我会给出最好的答案! - soneangel

1

叹气... 我写程序的时候,@jarnbjo 解释了同样的想法。但是这里有一些代码:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.NavigableMap;
import java.util.TreeMap;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageParser {
    public static void main(String[] args) {
        String dataContent = 
            "6 5\n" + 
            ".7 .7 .6 1.0 1.2 .1\n" + 
            ".9 .3 .7 1.1 .7 .2\n" + 
            "1 1.1 1.2 1.3 1.7 .6\n" + 
            ".5 .6 .5 .4 .9 .1101\n" + 
            "2 .1 .1 .1 2.1 1.1";

        String colorContent = 
            "0 .5 255 128 64\n" + 
            ".5 .75 128 255 32\n" + 
            ".75 1.25 64 64 225\n" + 
            "01.50 5 128 128 0";

        int width = 0;
        int height = 0;
        BufferedImage image = null;

        NavigableMap<Double, Integer> colorMap = new TreeMap<Double, Integer>();
        for (String colorLine : colorContent.split( "\n" )) {
            String[] colorValues = colorLine.split( " " );
            colorMap.put( Double.parseDouble( colorValues[1] ), 
                    Integer.parseInt( colorValues[2] ) << 16 | 
                    Integer.parseInt( colorValues[3] ) << 8 | 
                    Integer.parseInt( colorValues[4] ) );
        }
        boolean headerParsed = false;
        int y = 0;
        for( String dataLine : dataContent.split( "\n" ) ) {
            String[] dataArray = dataLine.split( " " );
            if( !headerParsed ) {
                width = Integer.parseInt( dataArray[ 0 ] );
                height = Integer.parseInt( dataArray[ 1 ] );
                image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
                headerParsed = true;
            }
            else {
                int x = 0;
                for( String data : dataArray ) {
                    Integer rgbValue = colorMap.higherEntry( Double.parseDouble( data ) ).getValue();
                    image.setRGB( x, y, rgbValue );
                    x++;
                }
                y++;
            }
        }

        JFrame frame = new JFrame();
        frame.getContentPane().add( new Viewer( image, width, height, 20 ) );
        frame.pack();
        frame.setVisible( true );
    }

    static class Viewer extends JPanel {
        Image m_image;
        int m_width;
        int m_height;
        int m_zoom;
        public Viewer( Image image, int width, int height, int zoom ) {
            m_image = image;
            m_width = width;
            m_height = height;
            m_zoom = zoom;
        }

        @Override
        public void paint(Graphics g) {
            g.drawImage( m_image, 0, 0, m_width * m_zoom, m_height * m_zoom, this );
        }
    };
}

0
在同一时间里(我需要更多时间哈哈),我只编写了将第一个矩阵文件保存到数组中的代码:(这是代码,如果您想看一下,它可以工作。但实际上我在使用你的代码。我会告诉你我达到了什么目标。谢谢大家...
import java.io.*;<br>
import java.util.StringTokenizer;

public class TokenizerUser4 {

public static double[][] matrix;

public static void main(String[] args) throws IOException {

    FileReader reader = new FileReader
        ("D:\\sonenos\\java\\FlussiIO\\new\\matrix.txt");       

    BufferedReader br = new BufferedReader(reader);

    String line;
    int rowIndex = 0;
    int counter = 0;
    int[] dim = new int[3]; 

    while ((line = br.readLine()) != null){

        counter++;

        if (counter == 1) {

            StringTokenizer dimensioni = new StringTokenizer(line);
            //int[] dim = new int[3];
            int i = 0;
            while(dimensioni.hasMoreTokens()){
                dim[i] = Integer.parseInt(dimensioni.nextToken());
                //System.out.println(dim[i] + " i=" + i);
                i++;

            }
            matrix = new double[dim[0]][dim[1]];
        }

        if (counter != 1){
            StringTokenizer theLine = new StringTokenizer(line);    

            int colIndex = 0;

            while (theLine.hasMoreTokens()){

                String st = theLine.nextToken();
                matrix[rowIndex][colIndex] = Double.parseDouble(st);

                colIndex = colIndex + 1;
            }

            rowIndex = rowIndex + 1;
        }               
    }

    for (int x = 0; x<dim[0];x++){
        for (int y = 0; y<dim[1]; y++){
            System.out.print(matrix[x][y] + " ");
        }
        System.out.println("\n");
    }

    br.close();     
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接