반응형
자바 웹캠 연동하기 2
녹화와 사진캡쳐 기능을 넣었다.
물론 내가 한건 아니다.
Xuggler Library가 다했다. 라이브러리가 근데 너무크다 38MB
Xuggle-Xuggler 5.4.jar 파일인데 Xuggler란 영상을 디코딩, 인코딩 하는 라이브러리라 한다.
아무튼 내가 만든것!
만든것이라 보기보다는 라이브러리 사용한것! 이라는게 맞겠지만.. ㅎㅎ
소스코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.image.BufferedImage; import java.lang.Thread.UncaughtExceptionHandler; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; import com.github.sarxos.webcam.Webcam; import com.github.sarxos.webcam.WebcamDiscoveryEvent; import com.github.sarxos.webcam.WebcamDiscoveryListener; import com.github.sarxos.webcam.WebcamEvent; import com.github.sarxos.webcam.WebcamListener; import com.github.sarxos.webcam.WebcamPanel; import com.github.sarxos.webcam.WebcamPicker; import com.github.sarxos.webcam.WebcamResolution; import java.awt.Dimension; import java.io.File; import java.io.IOException; import com.xuggle.ferry.Logger; import com.xuggle.ferry.Logger.Level; import com.xuggle.mediatool.IMediaWriter; import com.xuggle.mediatool.ToolFactory; import com.xuggle.xuggler.ICodec; import com.xuggle.xuggler.IPixelFormat; import com.xuggle.xuggler.IVideoPicture; import com.xuggle.xuggler.video.ConverterFactory; import com.xuggle.xuggler.video.IConverter; /** * * Proof of concept of how to handle webcam video stream from Java * * * * @author Bartosz Firyn (SarXos) * */ public class MainFrame extends JFrame implements Runnable, WebcamListener, WindowListener, UncaughtExceptionHandler, ItemListener, WebcamDiscoveryListener, ActionListener { /** * */ private Webcam webcam = null; private WebcamPanel panel = null; private WebcamPicker picker = null; private JButton picture_button; private JButton record_button; private JButton stop_button; private static final Logger LOG = Logger.getLogger(MainFrame.class.getName()); private volatile boolean running = false; Thread captureThread; private int i = 0; @Override public void run() { Webcam.addDiscoveryListener(this); setTitle("카메라 연동"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); picture_button = new JButton("사진찍기"); record_button = new JButton("녹화시작"); stop_button = new JButton("녹화종료"); picture_button.addActionListener(this); record_button.addActionListener(this); stop_button.addActionListener(this); addWindowListener(this); picker = new WebcamPicker(); picker.addItemListener(this); webcam = picker.getSelectedWebcam(); if (webcam == null) { System.out.println("카메라를 찾을수 없다..."); System.exit(1); } webcam.setViewSize(WebcamResolution.VGA.getSize()); webcam.addWebcamListener(MainFrame.this); panel = new WebcamPanel(webcam, false); panel.add(picker, BorderLayout.NORTH); panel.add(picture_button, BorderLayout.NORTH); panel.add(record_button, BorderLayout.NORTH); panel.add(stop_button, BorderLayout.NORTH); // panel.setFPSDisplayed(false); add(panel, BorderLayout.CENTER); pack(); setVisible(true); Thread t = new Thread() { @Override public void run() { panel.start(); } }; t.setName("example-starter"); t.setDaemon(true); t.setUncaughtExceptionHandler(this); t.start(); } public static void main(String[] args) { SwingUtilities.invokeLater(new MainFrame()); } @Override public void webcamOpen(WebcamEvent we) { System.out.println("카메라 열렸다."); } @Override public void webcamClosed(WebcamEvent we) { System.out.println("카메라 닫혔다."); } @Override public void webcamDisposed(WebcamEvent we) { System.out.println("카메라 꺼졌다."); } @Override public void webcamImageObtained(WebcamEvent we) { // do nothing } @Override public void windowActivated(WindowEvent e) { } @Override public void windowClosed(WindowEvent e) { webcam.close(); } @Override public void windowClosing(WindowEvent e) { } @Override public void windowOpened(WindowEvent e) { } @Override public void windowDeactivated(WindowEvent e) { } @Override public void windowDeiconified(WindowEvent e) { System.out.println("webcam viewer resumed"); panel.resume(); } @Override public void windowIconified(WindowEvent e) { System.out.println("webcam viewer paused"); panel.pause(); } @Override public void uncaughtException(Thread t, Throwable e) { System.err.println(String.format("Exception in thread %s", t.getName())); e.printStackTrace(); } @Override public void itemStateChanged(ItemEvent e) { if (e.getItem() != webcam) { if (webcam != null) { panel.stop(); remove(panel); webcam.removeWebcamListener(this); webcam.close(); webcam = (Webcam) e.getItem(); webcam.setViewSize(WebcamResolution.VGA.getSize()); webcam.addWebcamListener(this); System.out.println("selected " + webcam.getName()); panel = new WebcamPanel(webcam, false); // panel.setFPSDisplayed(false); add(panel, BorderLayout.CENTER); pack(); Thread t = new Thread() { @Override public void run() { panel.start(); } }; t.setName("example-stoper"); t.setDaemon(true); t.setUncaughtExceptionHandler(this); t.start(); } } } @Override public void webcamFound(WebcamDiscoveryEvent event) { if (picker != null) { picker.addItem(event.getWebcam()); } } @Override public void webcamGone(WebcamDiscoveryEvent event) { if (picker != null) { picker.removeItem(event.getWebcam()); } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == picture_button) { System.out.println("사진 찰칵"); // Webcam.setAutoOpenMode(true); BufferedImage image = Webcam.getDefault().getImage(); // save image to JPG file try { ImageIO.write(image, "JPG", new File("녹화사진.jpg")); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } if (e.getSource() == record_button) { // sarxos: make sure to disable button, otherwise it will // fail when clicked twice record_button.setEnabled(true); stop_button.setEnabled(true); running = true; captureThread = new Thread() { @Override public void run() { File file = new File("녹화파일.ts"); IMediaWriter writer = ToolFactory.makeWriter(file.getName()); Dimension size = WebcamResolution.VGA.getSize(); writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, size.width, size.height); webcam.open(); long start = System.currentTimeMillis(); System.out.println("녹화시작"); try { while (running) { // System.out.println("Capture frame " + i); BufferedImage image = ConverterFactory.convertToType(webcam.getImage(), BufferedImage.TYPE_3BYTE_BGR); IConverter converter = ConverterFactory.createConverter(image, IPixelFormat.Type.YUV420P); IVideoPicture frame = converter.toPicture(image, (System.currentTimeMillis() - start) * 1000); // frame.setKeyFrame(i == 0); frame.setQuality(0); writer.encodeVideo(0, frame); // 10 FPS try { Thread.sleep(100); } catch (InterruptedException ex) { // sarxos: in case of interrupted exception, // just break, // return or throw, it's not severe exception, // but you shall not // ignore it because your application can hang // in some rare // cases, especially when it's multithreaded // LOG.log(Level.SEVERE, null, ex); break; } } } finally { writer.close(); // sarxos: don't forgot to close writer // when done with recording } System.out.println("Video recorded in file: " + file.getAbsolutePath()); } }; // sarxos: if you are using worker threads (threads which perform // some // abstract parallel tasks) make sure to use daemon threads, // otherwise, // your app can be blocked when there are more than one non-daemon // thread // running which does not want to join captureThread.setDaemon(true); captureThread.start(); } else if (e.getSource() == stop_button) // stop { running = false; System.out.println("녹화 끝"); try { captureThread.join(); } catch (InterruptedException ex) { } } } } | cs |
필요한 라이브러리중 가장 대표적인게 xuggle-xuggler-5.4.jar 파일인데
https://mvnrepository.com/artifact/xuggle/xuggle-xuggler/5.4
위의 링크 들어가서 jar 누르면 파일이 다운된다. 다운받아서 사용하면 됨
반응형
'나는요 공부가 좋....은걸... > etc..' 카테고리의 다른 글
자바 웹캠 연동하기 (0) | 2018.03.21 |
---|