Domanda

ho per creare un'immagine BMP da due txt files.The primo è una matrice mxn:
* * * * * * * *
m n
C11 C21 .. cm1
...
C1N C2N .. CMN
* * * * * * * *
* * * * * * * *
6 5
0,7 0,7 0,6 1,0 1,2 0,1
0,9 0,3 0,7 1,1 0,7 0,2
1 1.1 1.2 1.3 1.7 .6
0,5 0,6 0,5 0,4 0,9 0,1101
2 .1 .1 .1 2.1 1.1
* * * * * * * *

Il secondo file txt è una scala di colori, come questo
* * * * * * * *
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
0,75 1,25 64 64 225
01.50 5 128 128 0
* * * * * * * *

Così ho di leggere da questi due file. Ho cercato di creare un array dal primo file txt utilizzando la classe StringTokenizer, ma mi sono perso a tutti. Dalle due file ho per creare un'immagine BMP. Qualcuno mi può aiutare in qualche modo?

È stato utile?

Soluzione

Sigh ... mentre ho scritto il programma verso il basso, @jarnbjo spiegato la stessa idea. Ma qui si ha un po 'di codice:

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

Altri suggerimenti

Se le gamme di colore sono in continuo (il vostro esempio manca 1,25-1,5) e sono garantiti per coprire tutti i valori possble nel file della matrice, desidero innanzitutto costruito un TreeMap<Double, java.awt.Color>, utilizzando il valore massimo dal file colore come mappa chiave . È quindi possibile utilizzare il metodo TreeMap#ceilingEntry(K) per ottenere il colore per ogni valore della matrice. Per esempio. se popolato correttamente con i dati di test, ceilingEntry (0,2) .getValue () restituirà colore (255,128,64).

Invece di leggere il file di matrice in un array, si potrebbe più facilmente utilizzare direttamente un java.awt.BufferedImage per disegnare in e in seguito utilizzare javax.imageio.ImageIO per scrivere l'immagine tamponato come un file BMP.

Nello stesso tempo (ho bisogno di più tempo lol) Ho scritto solo il codice per salvare nella matrice del primo file di matrice :( ecco il codice solo se si vuole dare un aspetto e funziona. Ma in realtà io' m sul vostro. io farò sapere quello che mi arriva. Grazie ragazzi ...

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();     
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top