-
How to resize attach image and view it (can see original size)?
almost 8 years ago
-
almost 8 years ago
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(); } }
-
1 Answer(s)