Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to resize attach image and view it (can see original size)?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 647
    Answer it

    Hye. I am Nabilah and below is my Java code at Jframe in netbeans. It is code about attach file or imageto display to Jframe.  Here I hope someone can help me

    1. how to resize image (stadardize the size I upload) that already attached and display to Jframe?

    2. How I can view that resize image and see the big or original size of image?

    3. How can I save my image or file attach in JFrame so if I close run windows that file or image still there and I can delete it if I want ?

    Thank you. I appreaciate Your help. ASAP.

    *******************************************************************************************************************

    private void jButton_attachimageActionPerformed(java.awt.event.ActionEvent evt) {                                                    
            // TODO add your handling code here:
            
            JFileChooser chooser=new JFileChooser();
            chooser.showOpenDialog(null);
            File f=chooser.getSelectedFile();
            String filename=f.getAbsolutePath();
            jTextField_path.setText(filename);
            ImageIcon icon=new ImageIcon(filename);
            jLabel_image.setIcon(icon);
        }          

                 

 1 Answer(s)

  • Hi
    You can use below code for re-size and uploading.

    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.awt.image.WritableRaster;
    import java.io.File;
    
    import javax.swing.ImageIcon;
     
    public class ImageUpload1  {
     
        private static final int IMG_WIDTH = 120;
        private static final int IMG_HEIGHT = 120;
    
        ImageIcon photo;
        WritableRaster raster;
        DataBufferByte data;
        File image;
     
                             
     
         void reSize() {                                         
             try {
                   image = new File("image path");
                    BufferedImage originalImage = ImageIO.read(image);
                    int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
     
                    BufferedImage resizeImageJpg = resizeImage(originalImage, type);
                    photo = new ImageIcon(toImage(resizeImageJpg));
     
                    //converting buffered image to byte array
                    raster = resizeImageJpg.getRaster();
                    data = (DataBufferByte) raster.getDataBuffer();
                   Save();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
              
        }                                        
        public Image toImage(BufferedImage bufferedImage) {
            return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
        }
        void Save() {                                        
            // declare a connection by using Connection interface 
            Connection connection = null;
            // Create string of connection url within specified format with machine 
            //name, port number and database name. 
     
            String connectionURL = "jdbc:mysql://localhost:3306/dbname";
     
            //declare a resultSet that works as a table resulted by execute a specified 
            //sql query.
            ResultSet rs = null;
     
            // Declare statement.
            PreparedStatement psmnt = null;
     
            try {
     
                //Load JDBC driver "com.mysql.jdbc.Driver"
                Class.forName("com.mysql.jdbc.Driver").newInstance();
     
                // Create a connection by using getConnection() method that takes 
               // parameters of string type connection url, user name and password to 
                //connect to database. 
                connection = DriverManager.getConnection(connectionURL, "root", "dbpass");
                // prepareStatement() is used for create statement object that is 
                //used for sending sql statements to the specified database. 
                psmnt = connection.prepareStatement("insert into save_image(name, city, image, Phone) "
                        + "values(?)");
               
                byte[] extractBytes = data.getData();
                psmnt.setBytes(1, extractBytes);
                // executeUpdate() method execute specified sql query. Here this query 
                //insert data and image from specified address. 
                int s = psmnt.executeUpdate();
                if (s > 0) {
                    System.out.println("Uploaded successfully !"); 
                 
                } else {
                    System.out.println("unsucessfull to upload image.");
                }
                connection.close();
                psmnt.close();
            } // catch if found any exception during rum time.
            catch (Exception ex) {
                System.out.println("Found some error : " + ex);
            }
        }                                       
     
       public BufferedImage resizeImage(BufferedImage originalImage, int type) {
            BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
            g.dispose();
     
            return resizedImage;
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
         new ImageUpload1().reSize();
        }
           
    }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: