Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Rotazione immagine

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2012
    Messaggi
    11

    Rotazione immagine

    Ciao a tutti ho due problemi con la rotazione di una immagine:

    data una BufferedImage im:

    int y=im.getHeight();
    g2.rotate(Math.toRadians(90));
    g2.drawImage(im, 0, -y*2, this);

    L'immagine viene ruotata correttamente però non riesco a capire come, dato questo codice, potrei salvare su file l'immagine ruotata tramite il metodo ImageIO.write perchè, l'immagine viene si ruotata ma solo sullo schermo e non viene salvata su nessun oggetto BufferedImage..come potrei fare??

    Un'altra opzione che ho trovato è questa:

    AffineTransform at = new AffineTransform();
    at.rotate(Math.toRadians(90),x/2,y/2);

    BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage bi=op.filter(im,null);
    g2.drawImage(bi, 0, 0,null);
    try {
    ImageIO.write(bi, "jpg", new File("r.jpg"));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    In questo caso riesco a salvarla su file, ma quando la stampa a schermo sul lato sinistro lascia uno spazietto bianco tra il bordo del frame e l'imm, mentre taglia un piccolo pezzo della parte nord dell'immagine(cosa che nel primo esempio non succedeva)..come mai?

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    allora, mettendo insieme un po' di righe di codice di altri progettini:
    codice:
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import javax.imageio.*;
    /**
     *
     * @author Andrea
     */
    public class ImageRotate extends JFrame {
        
        private File base_path = new File("C:/Users/Andrea/Desktop");
        private File source = new File(base_path, "source.jpg");
        private File dest = new File(base_path, "dest.jpg");
        
        private JLabel show_source, show_dest;
        
        private BufferedImage rotate(BufferedImage image, double degs) {
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage temp = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = temp.createGraphics();
            g2.rotate(Math.toRadians(degs), height / 2, height / 2);
            g2.drawImage(image, 0, 0, Color.WHITE, null);
            g2.dispose();
            return temp;
       }
        
        public ImageRotate() throws Exception {
            BufferedImage sourceImage = ImageIO.read(source);
            int sourceWidth = sourceImage.getWidth();
            int sourceHeight = sourceImage.getHeight();
            
            // Cosmetica
            int frameHeight = 40+Math.max(sourceHeight, sourceWidth);
            int frameWidth = 10 + Math.max(sourceHeight, sourceWidth);
            
            // rotazione e salvataggio
            BufferedImage destImage = this.rotate(sourceImage, 90);        
            ImageIO.write(destImage, "jpeg", dest);      
            
            // displaya  video.
            this.show_source = new JLabel(new ImageIcon(sourceImage));
            this.show_dest = new JLabel(new ImageIcon(destImage));
            
            JPanel showImages = new JPanel(new GridLayout(2,1));
            showImages.add(show_source); showImages.add(show_dest);
            
            this.getContentPane().add(new JScrollPane(showImages), BorderLayout.CENTER);        
            this.setSize(frameWidth, frameHeight);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        }
        
        
        public static void main (String[] args) {
            try {
                new ImageRotate();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    questo mi fa il suo degno lavoro.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

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.