Regularly obtain the screen of the target machine in CentOS
1. Add a scheduled task
Crontab-l
[Plain] view plaincopy
- */1 ***** sh/home/aaron/run. sh>/home/aaron/cron. log2> & 1
2. cat/home/aaron/run. sh
[Plain] view plaincopy
- #! /Bin/sh
- # Cd/home/aaron
- # CLASSPATH = $ CLASSPATH:/home/aaron/. gradle/cache/com. google. zxing/core/jars/core-3.2.1.jar
- # CLASSPATH = $ CLASSPATH:/home/aaron/. gradle/cache/com. google. zxing/javase/jars/javase-3.2.1.jar
- # CLASSPATH = $ CLASSPATH:/home/aaron/. gradle/cache/commons-lang/jars/commons-lang-2.6.jar
- # CLASSPATH = $ CLASSPATH:/home/aaron/. gradle/cache/joda-time/jars/joda-time-1.6.2.jar
- # ExportCLASSPATH
- # ExportDISPLAY =: 0
- # Java =/home/aaron/soft/jdk1.8.0/bin/java
- # $ Javacom/aaron/my/resources/Test
- ImageDir =/tmp/images
- Pre_check (){
- If [! -D "$ imageDir"]; then
- Mkdir-p "$ imageDir"
- Fi
- }
- Pre_check
- # CommandStr = "/tmp/tool/client. sh; cd $ imageDir; ls"
- # Destination ssh host $ commandStr> t.txt
- #Forxin((catt.txt)
- # Do
- # Scpuser @ host:/tmp/images/$ x/tmp/images
- # Ssh target host "rm $ imageDir/$ x"
- # Done
3. Place the following information on the target machine
Cat/tmp/tool/client. sh
[Plain] view plaincopy
- #! /Bin/sh
- ImageDir =/tmp/images
- BaseDir =$ (cd $ (dirname $0); pwd)
- Pre_check (){
- If [! -D "$ imageDir"]; then
- Mkdir-p "$ imageDir"
- Fi
- }
- Pre_check
- CLASSPATH = $ CLASSPATH: $ baseDir/core-3.2.1.jar
- CLASSPATH = $ CLASSPATH: $ baseDir/javase-3.2.1.jar
- CLASSPATH = $ CLASSPATH: $ baseDir/commons-lang-2.6.jar
- CLASSPATH = $ CLASSPATH: $ baseDir/joda-time-1.6.2.jar
- ExportCLASSPATH
- ExportDISPLAY =: 0
- Cd $ baseDir
- Java =/home/aaron/soft/jdk1.8.0/bin/java
- $ Javacom/aaron/my/resources/Test
For cat Test. java, refer to the snapshot method.
[Java] view plaincopy
- Packagecom. aaron. my. resources;
- Importjava. awt .*;
- Importjava. awt. Dimension;
- Importjava. awt. image. BufferedImage;
- Importjava. io. File;
- Importjava. io. IOException;
- Importjava. nio. file. FileSystems;
- Importjava. nio. file. Path;
- Importjava. util. HashMap;
- Importjava. util. Map;
- Importjavax. imageio. ImageIO;
- Importcom. google. gson. Gson;
- Importcom. google. zxing .*;
- Importcom. google. zxing. client. j2se. BufferedImageLuminanceSource;
- Importcom. google. zxing. client. j2se. MatrixToImageWriter;
- Importcom. google. zxing. common. BitMatrix;
- Importcom. google. zxing. common. HybridBinarizer;
- Importorg. apache. commons. codec. binary. Base64;
- Importorg. apache. commons. lang. StringUtils;
- Importorg. joda. time. DateTime;
- PublicclassTest {
- PrivatestaticfinalStringfromDir = "/tmp/images ";
- PrivatestaticfinalStringtoDir = "/mnt/shared/processedQRCodes ";
- PrivatestaticfinalStringPNG = ". png ";
- PrivatestaticfinalintPNG_SIZE_HEIGHT = 200;
- PrivatestaticfinalintPNG_SIZE_WIDTH = 200;
- Publicstaticvoidmain (String [] args ){
- Snapshot ();
- // Filedir = newFile (fromDir );
- // If (dir. exists () & dir. isDirectory ()){
- // File [] files = dir. listFiles ();
- // For (Filef: files ){
- // If (f. isFile () & f. getName (). toLowerCase (). endsWith (PNG )){
- // Stringres = decode (f );
- /// System. out. println (res );
- // If (StringUtils. isNotBlank (res )){
- //// Movetoprocesseddir
- // Booleanmoved = move (f, newFile (toDir + File. separator + f. getName (). substring (0, f. getName (). indexOf (". ") +" _ "+ res + PNG ));
- // System. out. println (moved = true? "Movedtoprocesseddir.": "Can 'tmovefile:" + f. getAbsolutePath ());
- //}
- //}
- //}
- //}
- Stringres = decode (newFile ("/mnt/shared/qrcodes/B .png "));
- // Encode ("http://baike.baidu.com/link? Url = BP7l08ZZZa7HBLL0g4RCuVsN3BlNbuokYRKCuUk8ZexPDjxSVNMXQq_08WI1xz5EUAjVrce4qgSisos9K-mq-q ");
- }
- Privatestaticbooleanmove (Filef, FiletoDir ){
- Returnf. renameTo (toDir );
- }
- /**
- * Parse Images
- */
- PublicstaticStringdecode (Filefile ){
- BufferedImageimage;
- Stringres = null;
- Try {
- Image = ImageIO. read (file );
- LuminanceSourcesource = newBufferedImageLuminanceSource (image );
- Binarizerbinarizer = newHybridBinarizer (source );
- BinaryBitmapbinaryBitmap = newBinaryBitmap (binarizer );
- Map <DecodeHintType, Object> hints = newHashMap <DecodeHintType, Object> ();
- // Hints. put (DecodeHintType. CHARACTER_SET, "UTF-8 ");
- Resultresult = newMultiFormatReader (). decode (binaryBitmap, hints); // decode the image
- StringresultStr = result. getText ();
- // System. out. println (resultStr );
- Stringt = resultStr. substring (resultStr. indexOf ("? ") + 3, resultStr. length ());
- Stringbase64 = newString (Base64.decodeBase64 (t ));
- // System. out. println (t );
- System. out. print (base64 + "---> ");
- DecodeResultdecodeResult = newGson (). fromJson (base64, DecodeResult. class );
- Res = decodeResult. getD ();
- } Catch (effectione ){
- // Movetoprocesseddir
- Booleanmoved = move (file, newFile (toDir + File. separator + file. getName (). substring (0, file. getName (). indexOf (". ") + (res = null? "": "_" + Res) + PNG ));
- System. out. println (moved = true? "Exceptionhappenedandmovedtoprocesseddir.": "Can 'tmovefile:" + file. getAbsolutePath ());
- // E. printStackTrace ();
- }
- Returnres;
- }
- Publicstaticvoidsnapshot (){
- StringfileName = newDateTime (). toString ("yyyyMMddHHmmss ");
- Dimensionscreensize = Toolkit. getdefatooltoolkit (). getScreenSize ();
- Doubleh = screensize. getHeight ();
- Doublew = screensize. getWidth ();
- Try {
- Robotrobot = newRobot ();
- // BufferedImagebi = robot. createScreenCapture (newRectangle (Double. valueOf (w-PNG_SIZE_WIDTH)/2 ). intValue (), Double. valueOf (h-PNG_SIZE_HEIGHT)/2 ). intValue (), PNG_SIZE_WIDTH, PNG_SIZE_HEIGHT); // capture the screen according to the specified area
- BufferedImagebi = robot. createScreenCapture (newRectangle (screensize); // capture the Screen Based on the specified area
- ImageIO. write (bi, "png", newFile (fromDir + File. separator + fileName + PNG ));
- } Catch (AWTExceptione ){
- E. printStackTrace ();
- } Catch (io1_tione ){
- E. printStackTrace ();
- }
- }
- Publicstaticvoidencode (Stringcontent ){
- Intwidth = 400; // Image Width
- Intheight = 400; // Image Height
- Stringformat = "png"; // image type
- Map <EncodeHintType, Object> hints = newHashMap <EncodeHintType, Object> ();
- Hints. put (EncodeHintType. CHARACTER_SET, "UTF-8 ");
- Try {
- BitMatrixbitMatrix = newMultiFormatWriter (). encode (content,
- BarcodeFormat. QR_CODE, width, height, hints); // generate a Matrix
- Pathpath = FileSystems. getDefault (). getPath (toDir, "code.png ");
- MatrixToImageWriter. writeToPath (bitMatrix, format, path); // output the image
- } Catch (effectione ){
- E. printStackTrace ();
- }
- }
- }
- ClassDecodeResult {
- PrivateStringa;
- PrivateStringb;
- PrivateStringc;
- PrivateStringd;
- PrivateStringv;
- PublicStringgetA (){
- Returna;
- }
- PublicvoidsetA (Stringa ){
- This. a =;
- }
- PublicStringgetB (){
- Returnb;
- }
- PublicvoidsetB (Stringb ){
- This. B = B;
- }
- PublicStringgetC (){
- Returnc;
- }
- PublicvoidsetC (Stringc ){
- This. c = c;
- }
- PublicStringgetD (){
- Returnd;
- }
- PublicvoidsetD (Stringd ){
- This. d = d;
- }
- PublicStringgetV (){
- Returnv;
- }
- PublicvoidsetV (Stringv ){
- This. v = v;
- }
- }