英文:
Can i save a screenshot taken from netbeans to Mysql
问题
我正在进行一个项目,决定将账单的屏幕截图保存到MySQL数据库中。但是用于截取屏幕截图的ImageIcon没有提供文件长度,因此setBinaryStream()方法无法正常工作。我尝试过pstmt.setBlob (1, (BLOB)icon),但仍然无法正常工作。
如果可以的话,您可以直接将截取的屏幕截图保存到数据库中,请告诉我如何操作。
或者我可以在NetBeans中创建并保存自定义账单吗?
try
{
Rectangle screen = new Rectangle (Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screen);
ImageIcon icon = new ImageIcon(capture);
int len = icon.getWidth();
ImageIO.write(capture, "jpg", new File("screenshot.jpg"));
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Sign_up.class.getName()).log(Level.SEVERE, null, ex);
}
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp", "root", "")) {
Statement at = con.createStatement();
String qt = "insert into picture (Pictures) values (?)";
PreparedStatement pstmt = con.prepareStatement(qt);
pstmt.setBinaryStream(icon, ...);
}
}
英文:
I am doing a project in which i decided to save screenshot of the bill to mysql database. But the ImageIcon used to take the screenshot does not provide the length of file so, the setBinaryStream() isn't working, i have tried pstmt.setBlob (1, (BLOB)icon), still not working.
Can you save the taken screenshot directly to database, if so please tell me.
Or can i create and save custom bills in netbeans.
try
{
Rectangle screen = new Rectangle (Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screen);
ImageIcon icon = new ImageIcon(capture);
int len = icon.getWidth();
ImageIO.write(capture,"jpg",new File("screenshot.jpg"));
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Sign_up.class.getName()).log(Level.SEVERE, null, ex);
}
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp","root","")) {
Statement at = con.createStatement();
String qt = "insert into picture (Pictures) values (?)";
PreparedStatement pstmt = con.prepareStatement(qt);
pstmt.setBinaryStream (icon,);
}
答案1
得分: 0
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.imageio.ImageIO;
public class ScreenShot {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
// Note: Don't pass passwords on the command line in real life.
System.err.println("Usage: java ScreenShot <dbUrl> <dbUser> <dbPwd>");
return;
}
// Capture the screenshot
Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screen);
// Write it to a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(capture, "jpg", baos);
// Insert the bytes into the database
Connection connection = DriverManager.getConnection(args[0], args[1], args[2]);
String sql = "insert into picture (Pictures) values (?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));
ps.execute();
// Select the image from the database.
sql = "select Pictures from picture where id = 1";
ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery(sql);
if (rs.next()) {
InputStream input = rs.getBinaryStream(1);
// Write the image to a file (for verification).
FileOutputStream fos = new FileOutputStream("screenshot.jpg");
byte[] buffer = new byte[1024];
while (input.read(buffer) > 0) {
fos.write(buffer);
}
input.close();
fos.close();
}
}
}
Note, this requires a table like below to exist in the default schema connected to.
CREATE TABLE test.picture (
id int AUTO_INCREMENT ,
Pictures MEDIUMBLOB,
PRIMARY KEY (id)
);
英文:
You can create a ByteArrayInputStream using the bytes of the screenshot, and then pass it to the prepared statement using setBinaryStream(int,InputStream) like this...
ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));
Here is a full program that takes a screenshot, writes it to a BLOB, and then reads it from the BLOB, and writes it to a file (for verification).
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.imageio.ImageIO;
public class ScreenShot {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
// Note: Don't pass passwords on the command line in real life.
System.err.println("Usage: java ScreenShot <dbUrl> <dbUser> <dbPwd>");
return;
}
// Capture the screenshot
Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screen);
// Write it to a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(capture, "jpg", baos);
// Insert the bytes into the database
Connection connection = DriverManager.getConnection(args[0], args[1], args[2]);
String sql = "insert into picture (Pictures) values (?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));
ps.execute();
// Select the image from the database.
sql = "select Pictures from picture where id = 1";
ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery(sql);
if (rs.next()) {
InputStream input = rs.getBinaryStream(1);
// Write the image to a file (for verification).
FileOutputStream fos = new FileOutputStream("screenshot.jpg");
byte[] buffer = new byte[1024];
while (input.read(buffer) > 0) {
fos.write(buffer);
}
input.close();
fos.close();
}
}
}
Note, this requires a table like below to exist in the default schema connected to.
CREATE TABLE test.picture (
id int AUTO_INCREMENT ,
Pictures MEDIUMBLOB,
PRIMARY KEY (id)
);
答案2
得分: 0
或许可以将图像数据直接保存到数据库(不建议),而是将文件保存在某个地方(比如远程文件托管服务)。然后在SQL语句中保存文件的链接。
英文:
Maybe instead of saving the image data directly to the database (not recommended) save the file somewhere (like a remote file hosting service). Then in the SQL statement save the link of the file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论