package com.samples;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.filechooser.FileNameExtensionFilter;
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.IOException;
import javax.imageio.ImageIO;
import java.sql.*;
import javax.swing.JOptionPane;
public class ImageUpload1 extends javax.swing.JFrame {
private static final int IMG_WIDTH = 120;
private static final int IMG_HEIGHT = 120;
JLabel label;
ImageIcon photo;
WritableRaster raster;
DataBufferByte data;
File image;
public ImageUpload1() {
initComponents();
jPanel2.setSize(120, 120);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
btnSave = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel1.setText("jLabel1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Image resize and upload demo");
jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.setLayout(new java.awt.BorderLayout());
btnSave.setText("Save Image");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(80, 80, 80))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(47, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(111, 111, 111)
.addComponent(btnSave)
.addContainerGap(200, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(24, 24, 24)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnSave)
.addContainerGap(56, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
chooser.setCurrentDirectory(image);
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
image = chooser.getSelectedFile();
jLabel2.setText(image.getAbsolutePath());
try {
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();
} catch (IOException e) {
System.out.println(e.getMessage());
}
jPanel2.removeAll();
label = new JLabel("", photo, JLabel.CENTER);
jPanel2.add(label);
repaint();
chooser.setCurrentDirectory(image);
}
}
public Image toImage(BufferedImage bufferedImage) {
return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// 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/staffdb";
//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", "");
// 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(?,?,?,?)");
psmnt.setString(1, "Abir");
psmnt.setString(2, "Dhaka");
psmnt.setString(4, "123456");
byte[] extractBytes = data.getData();
psmnt.setBytes(3, 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 !");
JOptionPane.showMessageDialog(rootPane, "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);
}
}
private static 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[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ImageUpload1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnSave;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}