使用Java實現(xiàn)簽字功能的示例代碼
實現(xiàn)了一個簡單的簽字功能,同時支持將簽字圖像保存為PNG格式和將簽字添加到PDF文檔中。在點擊“簽字”按鈕后,會彈出一個窗口,用戶可以在其中繪制簽名,繪制完成后可選擇保存為PNG圖片或?qū)⒑灻砑拥街付ǖ腜DF文檔中。
完整代碼
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
public class SignatureDemo {
public static void main(String[] args) {
SignatureFrame frame = new SignatureFrame();
frame.setVisible(true);
}
}
class SignatureFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPanel;
public SignatureFrame() {
setTitle("簽字");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
contentPanel = new JPanel();
setContentPane(contentPanel);
// 添加按鈕,用于觸發(fā)簽名事件
JButton button = new JButton("簽字");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 顯示簽名面板
SignaturePanel panel = new SignaturePanel();
int result = JOptionPane.showConfirmDialog(contentPanel, panel, "簽字", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
// 獲取簽名圖像并保存到本地
saveSignatureImage(panel.getSignatureImage());
}
}
});
contentPanel.add(button);
}
/**
* 保存簽名圖像到本地
*/
private void saveSignatureImage(BufferedImage image) {
// 打開文件選擇器
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(contentPanel);
if (result == JFileChooser.APPROVE_OPTION) {
// 獲取用戶選擇的文件路徑
File file = fileChooser.getSelectedFile();
// 將圖像保存為PNG格式
try (OutputStream out = new FileOutputStream(file)) {
ImageIO.write(image, "PNG", out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class SignaturePanel extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage signatureImage;
public SignaturePanel() {
setPreferredSize(new Dimension(400, 300));
setBackground(Color.WHITE);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
// 添加鼠標(biāo)監(jiān)聽器,用于繪制簽名
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// 創(chuàng)建新的圖像,用于繪制簽名
signatureImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = signatureImage.createGraphics();
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2));
g.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
g.dispose();
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
// 繪制簽名
Graphics2D g = signatureImage.createGraphics();
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2));
g.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
g.dispose();
repaint();
}
});
}
public BufferedImage getSignatureImage() {
return signatureImage;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (signatureImage != null) {
g.drawImage(signatureImage, 0, 0, null);
}
}
}
class PDFUtils {
/**
* 在PDF文檔中添加簽名
*/
public static void addSignatureToPDF(File pdfFile, BufferedImage signatureImage) throws IOException, DocumentException {
Rectangle pageSize = new Rectangle(PageSize.A4);
Document document = new Document(pageSize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile.getAbsolutePath() + ".signed.pdf"));
document.open();
PdfContentByte canvas = writer.getDirectContent();
// 將簽名圖像添加到PDF文檔中
Image image = Image.getInstance(signatureImage, null);
image.scaleToFit(100, 50);
float x = (document.right() - image.getScaledWidth()) / 2;
float y = (document.top() - image.getScaledHeight()) / 2;
canvas.addImage(image, image.getScaledWidth(), 0, 0, image.getScaledHeight(), x, y);
document.close();
writer.close();
}
}
方法補充
除了上文的方法,我們還可以使用JavaFX或者Swing來實現(xiàn)簽字功能,下面是實現(xiàn)方法,希望對大家有所幫助
JavaFX中可以通過使用Canvas實現(xiàn)簽字功能。
步驟如下:
1. 創(chuàng)建一個JavaFX應(yīng)用程序。
2. 創(chuàng)建一個Canvas對象。
3. 重寫鼠標(biāo)事件的方法。
4. 在鼠標(biāo)移動時,通過Canvas的GraphicsContext對象繪制線條。
代碼示例:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
// 創(chuàng)建畫布
Canvas canvas = new Canvas(500, 500);
GraphicsContext gc = canvas.getGraphicsContext2D();
// 設(shè)置畫筆的屬性
gc.setLineWidth(2);
gc.setStroke(Color.BLACK);
// 重寫鼠標(biāo)事件的方法
canvas.setOnMouseDragged(e -> {
double x = e.getX();
double y = e.getY();
gc.lineTo(x, y); // 繪制線條
gc.stroke(); // 描邊
});
// 創(chuàng)建主場景
Scene scene = new Scene(new Group(canvas), 500, 500);
// 顯示窗口
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Swing中可以通過使用JPanel實現(xiàn)簽字功能。
步驟如下:
1. 創(chuàng)建一個JFrame窗口。
2. 創(chuàng)建一個JPanel對象。
3. 重寫鼠標(biāo)事件的方法。
4. 在鼠標(biāo)移動時,通過JPanel的Graphics對象繪制線條。
代碼示例:
public class Main extends JFrame {
public Main() {
// 創(chuàng)建面板
JPanel panel = new JPanel();
// 設(shè)置面板的大小和背景色
panel.setPreferredSize(new Dimension(500, 500));
panel.setBackground(Color.WHITE);
// 重寫鼠標(biāo)事件的方法
panel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Graphics g = panel.getGraphics();
// 設(shè)置畫筆的屬性
((Graphics2D)g).setStroke(new BasicStroke(2));
g.setColor(Color.BLACK);
// 繪制線條
g.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
}
});
// 添加面板到窗口中
getContentPane().add(panel);
// 設(shè)置窗口的屬性
setTitle("簽字功能");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}到此這篇關(guān)于使用Java實現(xiàn)簽字功能的示例代碼的文章就介紹到這了,更多相關(guān)Java簽字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot報錯找不到自動注入的service問題
這篇文章主要介紹了解決springboot報錯找不到自動注入的service問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Java.lang.OutOfMemoryError: GC overhead limit
本文主要介紹了Java.lang.OutOfMemoryError: GC overhead limit exceeded錯誤的解決,錯誤是由于堆空間不足導(dǎo)致GC頻繁運行,從而引起的,下面就來介紹一下解決方法2025-03-03
Spring中的@Qualifier注解和@Resource注解區(qū)別解析
這篇文章主要介紹了Spring中的@Qualifier注解和@Resource注解區(qū)別解析,@Qualifier注解的用處是當(dāng)一個接口有多個實現(xiàn)的時候,為了指名具體調(diào)用哪個類的實現(xiàn),@Resource注解可以通過 byName命名和byType類型的方式注入,需要的朋友可以參考下2023-11-11
SpringBoot日程管理Quartz與定時任務(wù)Task實現(xiàn)詳解
定時任務(wù)是企業(yè)級開發(fā)中必不可少的組成部分,諸如長周期業(yè)務(wù)數(shù)據(jù)的計算,例如年度報表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報告,還有搶購類活動的商品上架,這些都離不開定時任務(wù)。本節(jié)將介紹兩種不同的定時任務(wù)技術(shù)2022-09-09

