91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用Java+OpenCV實現拍照功能

發布時間:2022-01-06 16:10:13 來源:億速云 閱讀:173 作者:iii 欄目:開發技術

本篇內容主要講解“如何用Java+OpenCV實現拍照功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何用Java+OpenCV實現拍照功能”吧!

依賴jar包:只有一個OpenCv的jar包,可以直接從官網上下載

如何用Java+OpenCV實現拍照功能

下面上代碼

import java.awt.Graphics;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;

public class CaptureBasic extends JPanel {undefined

    private static BufferedImage mImg;
    private static VideoCapture capture;
    private static  Mat capImg;

    private BufferedImage mat2BI(Mat mat){undefined
        int dataSize =mat.cols()*mat.rows()*(int)mat.elemSize();
        byte[] data=new byte[dataSize];
        mat.get(0, 0,data);
        int type=mat.channels()==1?
                BufferedImage.TYPE_BYTE_GRAY:BufferedImage.TYPE_3BYTE_BGR;

        if(type==BufferedImage.TYPE_3BYTE_BGR){undefined
            for(int i=0;i<dataSize;i+=3){undefined
                byte blue=data[i+0];
                data[i+0]=data[i+2];
                data[i+2]=blue;
            }
        }
        BufferedImage image=new BufferedImage(mat.cols(),mat.rows(),type);
        image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);

        return image;
    }

    public void paintComponent(Graphics g){undefined
        if(mImg!=null){undefined
            g.drawImage(mImg, 0, 0, mImg.getWidth(),mImg.getHeight(),this);
        }
    }

    public static void main(String[] args) {undefined
        try{undefined
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            capImg=new Mat();
            capture =new VideoCapture(0);
           // capture.open(0);
            int height = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
            int width = (int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
            if(height==0||width==0){undefined
                throw new Exception("camera not found!");
            }

            JFrame frame=new JFrame("camera");
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            CaptureBasic panel=new CaptureBasic();
            panel.addMouseListener(new MouseAdapter() {undefined
                @Override
                public void mouseClicked(MouseEvent arg0) {undefined
                     System.out.println("click");
                     saveImg();
                }
                @Override
                public void mouseMoved(MouseEvent arg0) {undefined
                    System.out.println("move");

                }
                @Override
                public void mouseReleased(MouseEvent arg0) {undefined
                    System.out.println("mouseReleased");
                }
                @Override
                public void mousePressed(MouseEvent arg0) {undefined
                    System.out.println("mousePressed");
                }
                @Override
                public void mouseExited(MouseEvent arg0) {undefined
                    System.out.println("mouseExited");
                    //System.out.println(arg0.toString());
                }
                @Override
                public void mouseDragged(MouseEvent arg0) {undefined
                    System.out.println("mouseDragged");
                    //System.out.println(arg0.toString());
                }});
            frame.setContentPane(panel);
            frame.setVisible(true);
            frame.setSize(width+frame.getInsets().left+frame.getInsets().right,
                    height+frame.getInsets().top+frame.getInsets().bottom);
            int n=0;
            Mat temp=new Mat();
            while(frame.isShowing()&& n<5){undefined
                System.out.println("第"+n+"張");
                capture.read(capImg);
                Imgproc.cvtColor(capImg, temp, Imgproc.COLOR_BGRA2BGR);
               
               // Imgcodecs.imwrite("C:/HWKJ/opencv/"+n+".png", temp); //自動保存
               
               // panel.mImg=panel.mat2BI(detectFace(capImg));
               
                panel.mImg=panel.mat2BI(capImg);
                panel.repaint();
               // n++;
                //break;
            }
            capture.release();
           // frame.dispose();
        }catch(Exception e){undefined
            System.out.println("例外:" + e);
        }finally{undefined
            System.out.println("--done--");
        }

    }
    
    //點擊面板
    public static void saveImg(){undefined
        Mat temp=new Mat();
        int height = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
        int width = (int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
        capture.read(capImg);
        Imgproc.cvtColor(capImg, temp, Imgproc.COLOR_BGRA2BGR);
        Imgcodecs.imwrite("C:/HWKJ/opencv/aaa.png", temp);
        System.out.println("保存圖片");
    }
}

如何用Java+OpenCV實現拍照功能

點擊在對應目錄下生成的照片

如何用Java+OpenCV實現拍照功能

到此,相信大家對“如何用Java+OpenCV實現拍照功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

石屏县| 盘山县| 西平县| 永年县| 营山县| 洛浦县| 克拉玛依市| 伽师县| 边坝县| 庄浪县| 松江区| 县级市| 桐庐县| 如东县| 普宁市| 金华市| 海南省| 临城县| 沾化县| 黄陵县| 神池县| 永和县| 林甸县| 繁昌县| 双城市| 昌图县| 昭苏县| 长春市| 金平| 香港| 中方县| 驻马店市| 中牟县| 绥棱县| 赤城县| 大港区| 英超| 宾阳县| 土默特左旗| 灵武市| 武义县|