使用Java實(shí)現(xiàn)簽字功能的示例代碼
實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的簽字功能,同時(shí)支持將簽字圖像保存為PNG格式和將簽字添加到PDF文檔中。在點(diǎn)擊“簽字”按鈕后,會(huì)彈出一個(gè)窗口,用戶可以在其中繪制簽名,繪制完成后可選擇保存為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();
}
}
方法補(bǔ)充
除了上文的方法,我們還可以使用JavaFX或者Swing來實(shí)現(xiàn)簽字功能,下面是實(shí)現(xiàn)方法,希望對(duì)大家有所幫助
JavaFX中可以通過使用Canvas實(shí)現(xiàn)簽字功能。
步驟如下:
1. 創(chuàng)建一個(gè)JavaFX應(yīng)用程序。
2. 創(chuàng)建一個(gè)Canvas對(duì)象。
3. 重寫鼠標(biāo)事件的方法。
4. 在鼠標(biāo)移動(dòng)時(shí),通過Canvas的GraphicsContext對(duì)象繪制線條。
代碼示例:
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)建主場(chǎ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實(shí)現(xiàn)簽字功能。
步驟如下:
1. 創(chuàng)建一個(gè)JFrame窗口。
2. 創(chuàng)建一個(gè)JPanel對(duì)象。
3. 重寫鼠標(biāo)事件的方法。
4. 在鼠標(biāo)移動(dòng)時(shí),通過JPanel的Graphics對(duì)象繪制線條。
代碼示例:
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實(shí)現(xiàn)簽字功能的示例代碼的文章就介紹到這了,更多相關(guān)Java簽字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot報(bào)錯(cuò)找不到自動(dòng)注入的service問題
這篇文章主要介紹了解決springboot報(bào)錯(cuò)找不到自動(dòng)注入的service問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java.lang.OutOfMemoryError: GC overhead limit
本文主要介紹了Java.lang.OutOfMemoryError: GC overhead limit exceeded錯(cuò)誤的解決,錯(cuò)誤是由于堆空間不足導(dǎo)致GC頻繁運(yùn)行,從而引起的,下面就來介紹一下解決方法2025-03-03
Spring中的@Qualifier注解和@Resource注解區(qū)別解析
這篇文章主要介紹了Spring中的@Qualifier注解和@Resource注解區(qū)別解析,@Qualifier注解的用處是當(dāng)一個(gè)接口有多個(gè)實(shí)現(xiàn)的時(shí)候,為了指名具體調(diào)用哪個(gè)類的實(shí)現(xiàn),@Resource注解可以通過 byName命名和byType類型的方式注入,需要的朋友可以參考下2023-11-11
Java從零實(shí)現(xiàn)超市會(huì)員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)超市會(huì)員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-12-12
Java設(shè)計(jì)模式中的簡(jiǎn)單工廠模式解析
這篇文章主要介紹了Java設(shè)計(jì)模式中的簡(jiǎn)單工廠模式解析,簡(jiǎn)單工廠模式提供一個(gè)創(chuàng)建對(duì)象實(shí)例的功能,而無須關(guān)心其具體實(shí)現(xiàn),被創(chuàng)建實(shí)例的類型可以是接口、抽象類,也可以是具體的類,需要的朋友可以參考下2023-11-11
SpringBoot日程管理Quartz與定時(shí)任務(wù)Task實(shí)現(xiàn)詳解
定時(shí)任務(wù)是企業(yè)級(jí)開發(fā)中必不可少的組成部分,諸如長(zhǎng)周期業(yè)務(wù)數(shù)據(jù)的計(jì)算,例如年度報(bào)表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報(bào)告,還有搶購(gòu)類活動(dòng)的商品上架,這些都離不開定時(shí)任務(wù)。本節(jié)將介紹兩種不同的定時(shí)任務(wù)技術(shù)2022-09-09

