Swing攝像頭拍照並截圖,等比縮放後儲存到資料庫後再顯示到桌面
阿新 • • 發佈:2018-12-11
一.功能介面展示:
二.程式碼展示:
以下是Swing窗體,按鈕設定。
/**攝像頭面板*/ static OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage(); private CanvasFrame canvas = new CanvasFrame("攝像頭");//新建一個視窗 OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); private JLabel canvasPanel = new JLabel(); // 照片顯示 private JPanel cameraPanel = new JPanel(); // 攝像頭JPanel /**開啟攝像頭按鈕*/ private JButton startButton = new JButton("開 啟"); /**拍攝攝像頭按鈕*/ private JButton cameraButton = new JButton("拍 攝"); /**關閉攝像頭按鈕*/ private JButton stopButton = new JButton("關 閉"); /**攝像頭面板*/ cameraPanel.setBackground(new Color(231, 230, 230)); cameraPanel.setBounds(getProportionWidth(630), getProportionHeight(15), getProportionWidth(260), getProportionHeight(280)); cameraPanel.setLayout(null); JPanel8.add(cameraPanel); //照片顯示面板 canvasPanel.setBounds(getProportionWidth(70), getProportionHeight(15), getProportionWidth(230), getProportionHeight(195)); canvasPanel.setBackground(new Color(231, 230, 230)); canvasPanel.setLayout(null); canvasPanel.setBorder(null); cameraPanel.add(canvasPanel); //預設顯示無照片 ImageIcon noimage = new ImageIcon(ClassLoader.getSystemResource("img/noimages.png")); canvasPanel.setIcon(noimage);
1.開啟按鈕程式碼:
功能:開啟攝像頭窗體,每過50秒獲取一次影象。
/**開始按鈕*/ startButton.setBackground(Color.white); startButton.setBounds(getProportionWidth(70), getProportionHeight(200), getProportionWidth(50), getProportionHeight(20)); startButton.setFont(new Font("華文新魏",Font.PLAIN,15)); cameraPanel.add(startButton); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { @Override public void run() { flag = true; while (flag){ try { grabber.start(); //開始獲取攝像頭資料 canvas.show();//顯示窗體 // canvas.setAlwaysOnTop(true); //頁面一直在最上面 canvas.setPreferredSize(new Dimension(600, 500));//設定窗體大小 canvas.setLocation(0, 0);//設定窗體位置 canvas.showImage(grabber.grab());//獲取攝像頭影象並放到視窗上顯示, 這裡的Frame frame=grabber.grab(); frame是一幀視訊影象 canvas.setResizable(false);// 靜止放大縮小 try { Thread.sleep(50);//50毫秒重新整理一次影象 } catch (InterruptedException e1) { e1.printStackTrace(); } } catch (FrameGrabber.Exception e1) { e1.printStackTrace(); } } } }).start(); } });
2.拍照按鈕程式碼
功能說明:
1.將圖片儲存到本地:在D盤新增一個資料夾,將擷取的圖片放入;
2.圖片處理:讀取本地資料夾的檔案,進行縮放處理後,儲存到資料庫;
3.展示圖片:讀取資料庫的圖片展現到介面上
4.刪除本地資料夾
5.關閉頁面,停止抓取
/**拍攝按鈕*/ cameraButton.setBackground(Color.white); cameraButton.setBounds(getProportionWidth(130), getProportionHeight(200), getProportionWidth(50), getProportionHeight(20)); cameraButton.setFont(new Font("華文新魏",Font.PLAIN,15)); cameraPanel.add(cameraButton); cameraButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { opencv_core.Mat mat = converter.convertToMat(grabber.grabFrame()); //在D盤新建一個資料夾 File file = new File("D:\\img"); file.mkdirs(); String filePath ="d:\\img\\"+ hyhMyJTF.getText().trim() + ".jpg";//圖片路徑 //將拍照的圖片放入資料夾中,並用會員號做檔名 opencv_imgcodecs.imwrite(filePath, mat); /**讀取本地圖片*/ // FileInputStream in; // in = ImageUtil.readImage(filePath); BufferedImage result = null; BufferedImage im = ImageIO.read(new File(filePath)); /** 原始影象的寬度和高度 */ int width = im.getWidth(); int height = im.getHeight(); //壓縮計算 /**這個引數是要轉化成的倍數,如果是1就是轉化成1倍*/ float resizeTimes = 0.5f; /** 調整後的圖片的寬度和高度 */ int toWidth = (int) (width * resizeTimes); int toHeight = (int) (height * resizeTimes); /** 新生成結果圖片 */ result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB); result.getGraphics().drawImage( im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null); /**將圖片轉成InputStream存入到資料庫*/ ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(result, "jpg", os); InputStream imageInput = new ByteArrayInputStream(os.toByteArray()); //儲存到資料庫中 VcvoyageBoardingRecodeDAO vcvoyageBoardingRecodeDAO = new VcvoyageBoardingRecodeDAO(); vcvoyageBoardingRecodeDAO.insertPicture(hyhMyJTF.getText().trim(),imageInput); //刪除本地檔案 if (file.exists()) {//判斷檔案是否存在 if (file.isFile()) {//判斷是否是檔案 file.delete();//刪除檔案 } else if (file.isDirectory()) {//否則如果它是一個目錄 File[] files = file.listFiles();//宣告目錄下所有的檔案 files[]; for (int i = 0;i < files.length;i ++) {//遍歷目錄下所有的檔案 files[i].delete();//把每個檔案用這個方法進行迭代 } file.delete();//刪除資料夾 } } else { System.out.println("所刪除的檔案不存在"); } //查詢圖片 V_membership v_membership = vcvoyageBoardingRecodeDAO.findPicture(hyhMyJTF.getText().trim()); Blob picture = v_membership.getM_picture();//獲取圖片 //Blob型別轉image,並顯示到系統中 InputStream is = picture.getBinaryStream(); //轉換成輸入流 BufferedImage img = ImageIO.read(is); Image images; images = img.getScaledInstance(300, 240, Image.SCALE_DEFAULT); canvasPanel.setIcon(new ImageIcon(images)); new Thread(()->{ try { Thread.sleep(1000); flag = false; canvas.dispose(); grabber.stop();//停止抓取 } catch (Exception e1) { e1.printStackTrace(); } }).start(); } catch (Exception e1) { e1.printStackTrace(); } } });
3.關閉按鈕程式碼
功能說明:關閉窗體,關閉抓取
/**關閉按鈕*/
stopButton.setBackground(Color.white);
stopButton.setBounds(getProportionWidth(190), getProportionHeight(200), getProportionWidth(50),
getProportionHeight(20));
stopButton.setFont(new Font("華文新魏",Font.PLAIN,15));
cameraPanel.add(stopButton);
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
flag = false;
canvas.dispose();
grabber.stop();//停止抓取
} catch (FrameGrabber.Exception e1) {
e1.printStackTrace();
}
}
});