Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    49

    Coma fondere piu jpeg in un unico jpeg????

    Ciao a tutti ho bisogno di una funz che fondi insieme piu immagini!!!!!

    Diciamo che splitto il file jpg con questa funzione:

    public static BufferedImage[] splitImage(BufferedImage img, int cols, int rows)
    {
    int w = img.getWidth()/cols;
    int h = img.getHeight()/rows;
    int num = 0;
    BufferedImage imgs[] = new BufferedImage[cols*rows];

    for(int y = 0; y < rows; y++)
    {
    for(int x = 0; x < cols; x++)
    {
    imgs[num] = new BufferedImage(w, h, img.getType());
    // Tell the graphics to draw only one block of the image
    Graphics2D g = imgs[num].createGraphics();
    g.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);
    g.dispose();
    num++;
    } //fine for interno
    } //fine for esterno
    return imgs;
    }//fine funz split

    Questa è la funzione che esegue il merge(suppongo di dividere l'immagine in nove pezzettini ...quindi gli passo width=3 height = 3):
    public static BufferedImage mergeImage(BufferedImage[] imgFrame, int width, int height)
    {
    int size = imgFrame.length;

    //suppongo frame colonne == righe
    int rows =(int) Math.sqrt(size);
    int cols = rows;

    BufferedImage Im = new BufferedImage(width, height, imgFrame[0].getType());

    int i=0;
    for( int Irows=0; Irows< rows; Irows++)
    {
    for(int Icols=0; Icols< cols; Icols++)
    {
    // chiedo a Graphics di disegnare su Im width x height
    Graphics2D g = Im.createGraphics();
    g.drawImage(Im, 0, 0, imgFrame.getWidth(), imgFrame.getHeight(), imgFrame.getWidth()*Irows, imgFrame.getHeight()*Icols, imgFrame.getWidth()*Irows+imgFrame.getWidth(), imgFrame.getHeight()*Icols+imgFrame.getHeight(), null);
    g.dispose();

    i++;
    }//fine for interno
    }//fine for esterno


    return Im;
    } //fine merge


    Sia Split che Merge funzionano solo che c'è un piccolo problema se a Merge passo il BufferedImage[] ritornato da split mi crea l'immagine tranquillamente...
    Ho bisogno di una funzione che mi faccia riottenere da dei file in jpeg salvati su disco un vettore di BufferedImage[] da passare a merge.....finora ho provicchiato ma mi esce una bella immagine NERA CHE PIU NERA NON SI PUO!!
    Qualcuno conosce qualche metodo o classe che danno la possibilità di fare queste operazioni sui jpeg?????


    thx in Anticipo per la risposta

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2011
    Messaggi
    51
    Se non ho capito male ti serve una funzione di questo tipo:

    codice:
        public static BufferedImage[] fromFilesToBufferedImages(File... files) throws IOException {
            
            BufferedImage[] images = new BufferedImage[files.length];
            
            for(int i = 0; i < images.length; i++) 
                images[i] = ImageIO.read(files[i]);
            
            return images;
        }
    La funzione statica read di ImageIO legge un file e lo trasforma in un oggetto BufferedImage.

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    49
    Ciao grazie sembra perfetta ehehehe:P...solo che un piccolo problema..la mia funzione merge
    ridisegna solo una parte dell'immagine e non capisco perchè:P!!!!!!

    Io gli passo un vettore di BufferedImage che contiene 4 elementi...ottenuti tramite la split
    come richiamo la merge mi ridisegna solo 1/4 (sono un po alle prime armi.....)

    Cmq la merge è questa non quella che ho postato sopra:
    public static BufferedImage mergeImage(BufferedImage[] imgFrame, int rows, int cols)
    {

    BufferedImage Im = new BufferedImage(imgFrame[0].getWidth() * rows, imgFrame[0].getHeight() *cols, imgFrame[0].getType()); // so che l'array è di un solo tipo
    int i=0; // devo operare su n elementi di BufferedImage[]..
    Graphics2D g = Im.createGraphics();

    for( int Irows=0; Irows< rows; Irows++)
    {
    for(int Icols=0; Icols< cols; Icols++)
    {
    // chiedo a Graphics di disegnare su Im width x height

    // disegno l'immaggine come un collage.....null ultimo parametro non ho bisogno di listener..
    g.drawImage(imgFrame[i], 0, 0, imgFrame[i].getWidth(), imgFrame[i].getHeight(), imgFrame[i].getWidth()*Irows, imgFrame[i].getHeight()*Icols, imgFrame[i].getWidth()*Irows+imgFrame[i].getWidth(), imgFrame[i].getHeight()*Icols+imgFrame[i].getHeight(), null);


    //incremento il valore ogni volta che disegno un frammento
    i++;

    }
    }
    g.dispose();

    return Im;
    }

    Sai dove sbaglio?

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2011
    Messaggi
    51
    L'errore principale sta nei valori che passi alla funzione

    codice:
    public abstract boolean drawImage(Image img,
    				      int dx1, int dy1, int dx2, int dy2,
    				      int sx1, int sy1, int sx2, int sy2,
    				      ImageObserver observer);
    infatti impostando i valori dx e dy come hai fatto tu disegnerà sempre nella stessa area. Io invece ti consiglio di utilizzare quest'altra drawImage:

    codice:
    public abstract boolean drawImage(Image img, int x, int y, 
    				      ImageObserver observer);
    [list=a][*]img -> l'immagine da disegnare[*]x e y -> le coordinate del punto dove iniziare a disegnare img[/list=a]


    codice:
    public static BufferedImage mergeImage(BufferedImage[] images, int rows, int columns)
    {
        if(rows * columns > images.length)
            throw new IllegalArgumentException();
        
        int maxWidth = 0,
            maxHeight = 0;
        
        for(BufferedImage img: images) {
            if(img.getWidth() > maxWidth)
                maxWidth = img.getWidth();
            
            if(img.getHeight() > maxHeight)
                maxHeight = img.getHeight();
        }
                
        
        
        BufferedImage targ = new BufferedImage(maxWidth * columns, maxHeight * rows, images[0].getType());
        
        Graphics2D g = targ.createGraphics();
        
        for(int r = 0; r < rows; r++)
            for(int c = 0; c < columns; c++)
                g.drawImage(images[r + c], c * maxWidth, r * maxHeight, null);
            
                
        g.dispose();
    
        return targ;	
    }
    Quindi ogni volta che disegni l'immagine devi fornirgli anche il nuovo punto dove disegnarla, e lo ricavi dalle dimensioni della img e dal numero di colonna e riga in cui devi collocare img.
    In questo caso la funzione crea una griglia che ha le posizioni della grandezza dell immagine più grande presente nell'array, ma con un paio di modifiche la si può rendere più "flessibile".

    Fammi sapere se va tutto bene

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    49
    Ah cacchio giusto :P!!!!! stavo impazzend....eheheheheh!!!
    (ho sostituito c+r con un contatore)
    Grazie Mille!!!!!!

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2011
    Messaggi
    51
    Prego

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.