이 기사는 일반적인 Java 프로그래밍 구현 스크린 샷 방법을 요약합니다. 다음과 같이 참조에 대해 공유하십시오.
방법 1 : 방법 1
import java.awt.desktop; import java.awt.dimension; import java.awt.rectangle; import java.awt.robot; import java.awt.toolkit; import java.aww t.image.bufferedimage; import java.io. 파일; import javax.imageio.imageio; public class 캡처 스크린 {public static void capturescreen (문자열 파일 이름, 문자열 폴더) 예외 {dimens ion screensize = toolkit.getDefaultToolKit (). getScreensize (); Robot = 새로운 로봇 (); = 새 파일 (screenfile, 폴더); imageio.write (image, "png", f); goodtop.getdesktop () .open (f); public static void main (string [] args) {capturescreen ( "e : // hello", "11.png"); / todo 자동 생성 캐치 블록 e.printstacktrace ()};방법 2 : 방법 2.
패키지 com.qiu.util; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; im port javax. imageio.*;/***간단한 화면 캡처***/public class screencapture {// 테스트 메인 퍼블릭 정적 void main (string [] args)은 예외 {string userDir = systeme m.getProperty ( "user.dir" 파일 tempfile = "d :", "temp.png"); Panel.setLayout (new BorderLayout ()); 캡처. } private screencapture () {robot = new robot ()} {System.err.println ( " + e); 대화. getContPane (); CP.SetLayout (New BorderLayout (); rech) 대화 상자 (false}); labfullscreenImage () {public void mousedRagged (mouseevent e vn) {if (isfirstpoint) {x1 = evn.getx (); (); isfirppoint} else {x2 = evn.gety (); int miny , recy, recw, reh); CURSOR .GetPredefinedCursor (Cursor.crossHair_Cursor); .getMaxumSize (); SETLETON 패턴 GetInstance () {return defaultCapturer}; 새로운 사각형 (getDefaultToolKit () .getScreensize ())); = Robot.createscreencapture (툴킷. getDefaultToolkit ()) 캡처* / public bufferedimage getPickedImage () {return pickedImage} / ** 캡처 된 아이콘* / public imageicon getPickedicon (return new ImageIcon) (getPickedImage ()); 형식 * * @deprecat ed는 saveaspng (파일) ** / @deprecated public void savetofile (파일 파일)으로 대체되었습니다 {imageio.write (getPickedImage (), defaultimageformater, file); png 형식*/ public void saveaspng (파일)는 ioexception {imageio.write (getPickedImage (), "png", jpeg 형식 이미지 파일*/ public void saveasjpeg (파일 파일)를 던집니다. Ioe xception {imageio.write (getPickedImage (), "jpeg", file) / ** write write a public void write (outputStream out) {imageio.write (getPickedIo.write, defaultima geformater, Out. }} // Singleton ScreenCapture 기본 캡처 (private screencapture); ndimage labfullscreenimage = 개인 bucketimage fullscreenimage; jlabel {public void paintcomponent (graphics g) {super.paintcomponent (x, y, w, h); ); Liney, getwidth (), int x, int y, int height) {h = y; } public void drawcross (int y) {linex = y;메소드 3 :
시스템 팔레트에 최소화되므로 이미지는 여전히 동일한 디렉토리에 배치해야합니다. 그렇지 않으면 포인터는 예외입니다.
주요 스크린 샷 코드 부분은 다음과 같습니다.
Robot Ro = new Robot (); 툴킷 tk = 툴킷 .getDefaultToolKit (); Dimension di = tk.getScreensize (); 사각형 Rec = New 사각형 (0,0, di.widt h, di.height); bufferedimage bi = ro CreateScReencapture (Rec);
(소스 코드는 CSDN에서 나왔습니다)
import java.awt.*; import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavore xception; import java.awt.event.*; import javax.swing. java.io.*; import java.awt.image jtabbedpane jtp; // 단일 장소 많은 그림 개인 int index; // 항상 증가하는 색인, 사진을 식별하는 데 사용되는 인덱스 개인 jradiobutton java, 시스템; // java 인터페이스, 시스템 인터페이스/** 캡처 스크린의 새로운 인스턴스를 만듭니다* / : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : setlookandfeel (uimanager.getsystemlookandfeelclassname ()) {exe.printstacktrace (); Top, jtabbedpane.scroll_tab_layout)} start = new Jbutton = 새로운 jbutton ( "Save all"); adcealllistener (adceallistener) (jpanel b uttonjp); Jlabel ( "스크린 캡처", Jlabel.Center); buttonjp.add (saveall); JPANEL (); // 두 개의 라디오 버튼이있는 패널 jp.add (Java = New Jradiobutton ( "Java Interface") (System = New Jradiobutton ( "System Interface"); adceptory 새 JPANEL (); : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :. this.setsize (500,400); {public void windowclosing (windowevent we) {capturescreen.setvisible (false}) {systemtray st = systemtray.getsystemtray (); () .getResource ( "bg.gif"); popupmenu ( "팝업 메뉴"); ActionEvent ae) {joptionpane.showmessagedialog (capturescreen.this, "<html> <font color = red> <center> <h 2> 정보 </h2> </center> </font>" + "이것은 순수한 Java입니다. 스크린 캡처 프로그램 <br> 이전 기준으로, 일반적으로 사용되는 일부 기능, 예를 들어, 배치 저장, 여러 클립 및 시스템 페이스트 보드에 복사하십시오. <br> " +" 사용 중에 질문이 있으시면 언제든지 문의하십시오. <br> " +"<font size = 5 color = blue> 저자 : Qianli bingfeng <br> " +"<i> qq : 24325142 </i> <br> </font> </html> ");}}); pm.addseparator (); pm.add (new menuitem ("show main window ")). addActionListener (new ActionListener () {public void ActionPerformed (ActionEvent AE) {capturescreen.this.setvisible (true)}; pm.add (new menuitem ( "exit program")). addactionListener (new ActionListener () {public void actionPerformed (ActionEvent Ae) {System.exit (0);}); "Java 스크린 캡처", PM); 예외적 인 경우 {exe.printstacktrace ()} private void updates () {true (get! = null); // 현재는 지우고 탭 페인이 다시 들어갑니다. 탭 패널에 직접 추가 할 필요가 없습니다. PIC); SWINKUTIFINS (C)}} {Try {False.Sleep (500); Ro = 툴킷 tk = getDefaultToolkit (); (rec); (true) jf 모든 그림 */ public void dosaveall () {if (jtp.getTabcount () == 0) {joptionpane.showmessagedialog ( "그림은 비어있을 수 없습니다!", "오류", joptionpane.error_message; } JFC = JFC.AddChoosableFileFilter (); (새로운 jpgfilter () ); JFC는 int i = jfc ext = tolowercase (); about = "png"; {ext = ext.Substring (0, ext.lastIndexof (ab out.tolowercase ())}; }} // 모든 그림을 저장하는 데 특별히 사용되는 스레드 클래스는 저장된 진행 막대 개인 클래스 Saveallthread를 표시해야합니다. jdialog empless runnable {private string header private string ext; // file format private jprogressbar J PB; // 진행률 표시 줄 개인 JLABEL 정보; // 정보 표시 표시 줄 개인 INT AllTask, Donetask; // 모든 작업, 완료된 작업 공개 SaveallThread (문자열 이름, 문자열 Ext) {SUPER (Capturescreen.This, "Save" , this .setminimum (0); addd (this.set.setsize); ) .Start (); private void setProgressBarstring () {jpb.setString ( "+donetask+"/"+alltask); {picpanel pp = (picpanel) jtp.getComponentat (i); setText (</b> <b> 저장 </b> <b> "+f.tostring ()+"</html> "; setValue (donetask). this.dispose ()}}} / ***공개 프로세스 및 사진 저장 방법*이 메소드는 더 이상 개인이 아닙니다* / public void dosave (bufferedImage get) {try {if (get == null) {joptionpane.showmessagedialog. 이것은 "오류가 될 수 없다", Jopti Onpane; 새로운 bmpfilter (); jfc.addchoosablefilter (); .getSelectedFile ()에 대한 문자열; jpg "; if (! ext.endswith (". jpg "))) {문자열 ns = ext+". if (! ext.endswith ( ". png") {string ns = ext+".png"; ext.endswith ( ". bmp") {string ns = ext+"; ( ".gif")) {string ns = ext+". gif"; if (imageio.write (get, about, about)) {joptionpane.showmessagedialog ! ");} else joptionpane.showmessagedialog (이것은"저장 실패! ");}} catch (예외 exe) {exe.printstacktrace ();}} /** *공개 프로세싱은 클립 보드 메소드에 현재 이미지를 추가합니다 * / public void docopy (최종 BufferedImage 이미지) {if (get == null) {joptionpane.showmessagedialog ( "이미지가 비어있을 수 없습니다!", "오류", error_message); new Transferable () {public dataflaver [] gettransferdataflavors () {return new dataflaver [] {dataflavor.imageflaver}; Flaver)는 지원되지 않는 플라보 로스크를 던졌습니다. if (iSdataflavorsupported (flavor)) reture xception (flavor); , "시스템 페이스트 보드에 복사하십시오!"} catch (Exception Exe) {exe.printstacktrace (); error_message}}} // COMPONTE C). getSource (); if (source == start) {dostart (); uimanager .getCrossPlatformlookandfeelclassname ()); swing. 그리고 탭 패널 패널에 넣을 수 있습니다. // 개인 클래스 저장 및 복사를위한 고유 한 방법 세트가 있습니다. Picpanel은 JPanel Ampess ActionListener {Jbutton 저장, 복사, Close; // bufferedimag, 저장, 복사, Close, Close를 나타내는 BufferedImag를 확장합니다. 버튼 BufferedImag e; public picpanel get (bufferedimage get) {get. get; ) {새 JBUTTON ( "SABE (s)"; '). Jlab el Icon = New Zlabel (get); (this); AddactionListener (this); ) {docopy (get); () {public boolean accept (file) {if (file.tostring (). endswith ( ". bmp") {return true} public; String getde script () {return " *.bmp (bmp image)";}}}; file) {if (file.toString () .tolowercase (). endswith ( ". j pg (jpg image) ";}} // 필터 저장 필터는 GIF 형식의 필터 저장 개인 클래스 giffilter javax.swing.filechooser.filefilter {public giffilter () {} public boolean accept (파일 파일) {if (fi le.toString (fi le.toString) ) .TOLOWERCASE () ENDSWITH ( ".gif"). // PNG 형식 필터 개인 클래스 PNGFILTER 확장 javax.swing.filechooser.filefilter {public boolean accept (파일 파일) {if (file.toString (). tolowercase (). endswith ( ". png") | isdirectory ()) {return} false; mouselistener, mouseMotionlistener {private bufferedImage bi; /선택한 영역을 나타냅니다. 개인 커서 CS = 새 커서 (cursor.crosshair_cursor); // 일반 개인 상태의 마우스 상태를 표시합니다. 8 개의 편집 지점/ : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : /The following four constants indicate who is the endpoint on the selected line. 공개 최종 int start_x = 1; 이 두 가지만 개인 포인트를 변경하면 필요합니다. 더 이상 공개 온도 (Jframe JF, BufferedImage bi, int 높이) {this.width = height; addmouseMotionListener (this) {rec = 0; i <rec.length; i ++); )}} public void paintcomponent (G.DrawImage) (bi, 0,0, 높이, this); G.Drawline (startx, endx, endy) (startx, startx, endx); endx; int y = endy? select = new 사각형 (x, y, math.abs) : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : (startx+endx)/2; 2,5,5); -2,5,5); 스타디 -5,5) G.FillRect (ENDX-2, 2,5,5); ] = 새로운 사각형 (x1-5, y-5,10,10); ] = 새로운 사각형 ((startx> endx? startx : endx) -5, y1-5,10,10) = 새로운 사각형 ((startx> endx? startx : endx) -5, (strasty> endy ? X-5, (Starty> Endy? Starty : Endy) -5,10, 10); (Color.cyan); G.DrawString ( "왼쪽 마우스 버튼을 누르고 스크린 샷 영역을 배치하지 마십시오", px, p.y+15) // 남동쪽과 같은 8 개의 방향으로 수정되도록 X와 Y의 좌표를 방어합니다. , West 등. break; Currenty = (starty? start_y); = (starty? end_y); start_x; mousefevend (mouseevent me) {ini tselect (showtip); 개인 무효가있는 영역 (mouseevent me) {if (me.getpoint ()) {this.setcursor (cursor.move_cursor); else {states [] st = states.values (); [i]; setcursor (st [i])}}}; Mouseevent Me) {} 공개 무효 인 Mousedrag ged (mouseevent me) {int x = me.getx (); Starty+= (y-tempy) (x-tempx); ) {currentx == start_x) {tempx =}; | 현재. current == states == states.north_east || Y; tempx = x {startx = tempy = me.g ety (); = false; tempx () emppy (); p = endx = 0; mouseevent me) {if (me.getClickCount () == 2) {// 새 사각형 (star tx, Starty, Math.abs (endx -startx), math.abs (endy -starty)); = me.getPoint (); if (select.contains (p)) {if (select.x+select.width <this.getWidth () && select .getSubimage (select.x, select.width, jf.dispose} else}; x+ select.width> = this.getWidth ()) {wid = this.getWidth ()-select.x; ) get = bi.x, select.y, wid, het); []) {SwingUtilities.invokelater (new Runnable () {public void run () {new capturescreen ();}}}}; // North (New Cursor (Cursor.n_resize_cursor)), North_east (New Cursor (Cursor.ne _resize_cursor))), East (New Cursor (Cursor .E_Resize_Cursor)), South_east (New Cursor (Cursor.se_resize_cursor)를 나타냅니다. ), South (New Cursor (Cursor.s_resize_cursor))), South_west (New Cursor (Cursor.sw_ resize_cursor)), West (New Cursor (cursor.w_resize_cursor)), Move (새 커서 (cursor.move_cursor)), default (new) 커서 (cursor.default_cursor);이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.