如何从txt文件中存储的rgb数据创建bmp文件?

发布于 2024-08-06 21:34:31 字数 704 浏览 6 评论 0原文

我必须从两个 txt 文件创建一个 bmp 图像。第一个是 mxn 数组:

* * * * * * * *
米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文件是色标,像这样

* * * * * * * *
最小值1 最大值1 r1 g1 b1
最小值2 最大值2 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 文件创建一个数组,但我完全迷失了。我必须从这两个文件创建一个 bmp 图像。 有人可以以某种方式帮助我吗?

I've to create a bmp image from two txt files.The first one is an mxn array:

* * * * * * * *
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
* * * * * * * *

The second txt file is a color scale, like this

* * * * * * * *
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
* * * * * * * *

So I've to read from this two files. I've tried to create an array from the first txt file using StringTokenizer class but I'm lost at all. From the two files I've to create a bmp image.
Someone can help me in some way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吖咩 2024-08-13 21:34:31

如果颜色范围是连续的(您的示例缺少 1.25-1.5)并且保证覆盖矩阵文件中的所有可能值,我首先会构建一个 TreeMap,使用颜色文件中的最大值作为贴图键。然后,您可以使用 TreeMap#ceilingEntry(K) 方法获取任何矩阵值的颜色。例如,如果正确填充了测试数据,ceilingEntry(0.2).getValue() 将返回 Color(255,128,64)。

您可以更轻松地直接使用 java.awt.BufferedImage 进行绘制,然后使用 javax.imageio.ImageIO 写入矩阵文件,而不是将矩阵文件读取到数组中缓冲图像为 BMP 文件。

If the colour ranges are continous (your example is missing 1.25-1.5) and are guaranteed to cover all possble values in the matrix file, I would first have built a TreeMap<Double, java.awt.Color>, using the max value from the colour file as map key. You can then use the TreeMap#ceilingEntry(K) method to get the colour for any matrix value. E.g. if populated correctly with your test data, ceilingEntry(0.2).getValue() will return Color(255,128,64).

Instead of reading the matrix file into an array, you could more easily directly use a java.awt.BufferedImage to draw into and later use javax.imageio.ImageIO to write the buffered image as a BMP file.

情话难免假 2024-08-13 21:34:31

叹息......当我写下程序时,@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 );
        }
    };
}

Sigh... while I wrote the program down, @jarnbjo explained the same idea. But here you have some code:

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 );
        }
    };
}
北凤男飞 2024-08-13 21:34:31

与此同时(我需要更多时间哈哈)我只编写了将第一个矩阵文件保存到数组中的代码:(如果您想看一下并且它可以工作,那么这里是代码。但实际上我在您的代码上.我会让你知道我会达到什么目的...

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();     
}
}

In the same while (I need more time lol) I wrote just the code to save into the array the first matrix file :( here is the code just if you want to give a look and it works. But actually I'm on yours. I'll let you know what I'll reach. Thank you guys...

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();     
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文