Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Ciao trudi1990, Domanda un pò generica non trovi ? Comunque quello che ti può sicuramente interessare è la Classe BufferedImage, ti posto il seguente link che sicuramente troverai utile.
    Ciao

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    miracoli della ricerca in internet:
    http://www.java2s.com/Code/Java/2D-G.../Imagecrop.htm

    il primo risultato di: java image crop
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    ed io avrei bisogno dei 6 numeri del superenalotto, anche se l'hanno beccato l'altro giorno. Dalle mie parti si dice che ci sono più giorni che salsicce (traduzione in vernacolo di aiutati che il ciel t'aiuta)
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    Non è proprio quello che cerchi, ma potrebbe esserti d'aiuto.

    http://forum.html.it/forum/showthread/t-1444949.html

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2011
    Messaggi
    51
    Credo che sia quello che cerchi. Non ho creato il pulsante per dare l'ok per il taglio dell'immagine (lo lascio a te xD), per il resto dopo aver rilasciato il mouse disegna un rettangolo desiderato e dopo 1,5 secondi taglia l'immagine e la salva.

    codice:
    package cutapp;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Federico Figus
     */
    public class CutApp {
    
        public static void main(String[] args) throws IOException {
            BufferedImage img = ImageIO.read(new File("bgd.jpg"));
            
            JFrame frame = new JFrame("Cut App");
            
            frame.setSize(800, 800);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            frame.add(new CutPanel(img));
            
            frame.setVisible(true);
        }
    }
    
    class CutPanel extends JPanel {
        
        private BufferedImage background;
        
        private Point start;
        private int width;
        private int height;
    
        public CutPanel(BufferedImage background) {
            this.background = background;
            
            setSize(background.getWidth(), background.getHeight());
            
            init();
        }
        
        
        private void init() {
            this.addMouseListener(new MouseListener(){
    
                @Override
                public void mouseClicked(MouseEvent e) {
                   
                }
    
                @Override
                public void mousePressed(MouseEvent e) {
                    start = e.getPoint();
                    
                    System.out.println(start);
                }
    
                @Override
                public void mouseReleased(MouseEvent e) {
                    
                    Point end = e.getPoint();
                    
                    width = Math.abs(start.y - start.x);
                    height = Math.abs(end.y - start.y);
                    
                    if(start.x > end.x)
                        start.x = end.x;
                    
                    if(start.y > end.y)
                        start.y = end.y;
                    
                    Graphics g = CutPanel.this.getGraphics();
                    
                    g.setColor(Color.red);
                    g.drawRect(start.x, start.y, width, height);
                    
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(CutPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                    try {
                        ImageIO.write(cutImage(background, start, width, height), "jpg", new File("cuttedImage.jpg"));
                    } catch (IOException ex) {
                        Logger.getLogger(CutPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
    
                @Override
                public void mouseEntered(MouseEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet.");
                }
    
                @Override
                public void mouseExited(MouseEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet.");
                }
            });
        }
            
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            
            g.drawImage(background, 0, 0, this);
        }
        
        public static BufferedImage cutImage(BufferedImage img, Point start, int width, int height) {
            
            BufferedImage target = new BufferedImage(width, height, img.getType());
            for(int y = 0; y < height; y++)
                for(int x = 0; x < width; x++) 
                    target.setRGB(x, y, img.getRGB(x + start.x, y + start.y));
            
            return target;        
        }
        
    }

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2011
    Messaggi
    51
    Si c'è un piccolo errore nella riga di codice dove calcola la larghezza della selezione. Comunque ho trovato un esempio sul sito della oracle sui JScrollPane e l'ho integrato con la mia classe, adesso dovrebbe andare

    class ScrollablePicture extends JLabel
    implements Scrollable,
    MouseMotionListener {

    private int maxUnitIncrement = 1;
    private boolean missingPicture = false;

    private Point start;
    private int width;
    private int height;

    private BufferedImage image;

    public ScrollablePicture(BufferedImage i, int m) {
    super(new ImageIcon(i));
    image = i;
    if (i == null) {
    missingPicture = true;
    setText("No picture found.");
    setHorizontalAlignment(CENTER);
    setOpaque(true);
    setBackground(Color.white);
    }
    maxUnitIncrement = m;

    //Let the user scroll by dragging to outside the window.
    setAutoscrolls(true); //enable synthetic drag events
    addMouseMotionListener(this); //handle mouse drags

    this.addMouseListener(new MouseListener(){

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
    start = e.getPoint();

    System.out.println(start);
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    Point end = e.getPoint();

    System.out.println(end);

    width = Math.abs(end.x - start.x);
    height = Math.abs(end.y - start.y);

    System.out.println(width + " " + height);
    if(start.x > end.x)
    start.x = end.x;

    if(start.y > end.y)
    start.y = end.y;

    Graphics g = ScrollablePicture.this.getGraphics();

    g.setColor(Color.red);
    g.drawRect(start.x, start.y, width, height);

    try {
    Thread.sleep(1500);
    } catch (InterruptedException ex) {
    Logger.getLogger(ScrollablePicture.class.getName() ).log(Level.SEVERE, null, ex);
    }

    try {
    ImageIO.write(cutImage(image, start, width, height), "jpg", new File("cuttedImage.jpg"));
    } catch (IOException ex) {
    Logger.getLogger(ScrollablePicture.class.getName() ).log(Level.SEVERE, null, ex);
    }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
    //throw new UnsupportedOperationException("Not supported yet.");
    }
    });

    }
    public static BufferedImage cutImage(BufferedImage img, Point start, int width, int height) {

    BufferedImage target = new BufferedImage(width > 0 ? width : 1, height > 0 ? height : 1, img.getType());
    for(int y = 0; y < height; y++)
    for(int x = 0; x < width; x++)
    target.setRGB(x, y, img.getRGB(x + start.x, y + start.y));

    return target;
    }


    //Methods required by the MouseMotionListener interface:
    @Override
    public void mouseMoved(MouseEvent e) { }
    public void mouseDragged(MouseEvent e) {
    //The user is dragging us, so scroll!
    Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
    scrollRectToVisible(r);
    }

    public Dimension getPreferredSize() {
    if (missingPicture) {
    return new Dimension(320, 480);
    } else {
    return super.getPreferredSize();
    }
    }

    public Dimension getPreferredScrollableViewportSize() {
    return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect,
    int orientation,
    int direction) {
    //Get the current position.
    int currentPosition = 0;
    if (orientation == SwingConstants.HORIZONTAL) {
    currentPosition = visibleRect.x;
    } else {
    currentPosition = visibleRect.y;
    }

    //Return the number of pixels between currentPosition
    //and the nearest tick mark in the indicated direction.
    if (direction < 0) {
    int newPosition = currentPosition -
    (currentPosition / maxUnitIncrement)
    * maxUnitIncrement;
    return (newPosition == 0) ? maxUnitIncrement : newPosition;
    } else {
    return ((currentPosition / maxUnitIncrement) + 1)
    * maxUnitIncrement
    - currentPosition;
    }
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
    int orientation,
    int direction) {
    if (orientation == SwingConstants.HORIZONTAL) {
    return visibleRect.width - maxUnitIncrement;
    } else {
    return visibleRect.height - maxUnitIncrement;
    }
    }

    public boolean getScrollableTracksViewportWidth() {
    return false;
    }

    public boolean getScrollableTracksViewportHeight() {
    return false;
    }

    public void setMaxUnitIncrement(int pixels) {
    maxUnitIncrement = pixels;
    }
    }
    http://download.oracle.com/javase/tu...crollpane.html

    http://download.oracle.com/javase/tu...lePicture.java


  7. #7
    Utente di HTML.it
    Registrato dal
    May 2011
    Messaggi
    51
    ti posto anche il mio main:

    [QUOTE]
    public static void main(String[] args) throws IOException {
    BufferedImage img = ImageIO.read(new File("bgd.jpg"));

    JFrame frame = new JFrame("Cut App");

    frame.setSize(600, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(new GridLayout());


    JPanel pan = new JPanel();

    JScrollPane scrollPanel = new JScrollPane(pan, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    pan.add(new ScrollablePicture(img, 5));

    frame.add(scrollPanel);


    frame.setVisible(true);
    }
    [\QUOTE]

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.