1、添加计划任务
crontab -l
*/1 * * * * sh /home/aaron/run.sh >> /home/aaron/cron.log 2>&1
#!/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/commons-lang/jars/commons-lang-2.6.jar #CLASSPATH=$CLASSPATH:/home/aaron/.gradle/cache/joda-time/joda-time/jars/joda-time-1.6.2.jar #export CLASSPATH #export DISPLAY=:0 #java=/home/aaron/soft/jdk1.8.0/bin/java #$java com/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" #ssh 目标机器host $commandStr > t.txt #for x in $(cat t.txt) #do #scp user@host:/tmp/images/$x /tmp/images #ssh 目标机器host "rm $imageDir/$x" #done
cat /tmp/tool/client.sh
#!/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 export CLASSPATH export DISPLAY=:0 cd $baseDir java=/home/aaron/soft/jdk1.8.0/bin/java $java com/aaron/my/resources/Test
cat Test.java 参考snapshot方法
package com.aaron.my.resources; import java.awt.*; import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.gson.Gson; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.joda.time.DateTime; public class Test { private static final String fromDir = "/tmp/images"; private static final String toDir = "/mnt/shared/processedQRCodes"; private static final String PNG = ".png"; private static final int PNG_SIZE_HEIGHT = 200; private static final int PNG_SIZE_WIDTH = 200; public static void main(String[] args) { snapshot(); // File dir = new File(fromDir); // if(dir.exists() && dir.isDirectory()){ // File[] files = dir.listFiles(); // for(File f:files){ // if(f.isFile()&&f.getName().toLowerCase().endsWith(PNG)){ // String res = decode(f); //// System.out.println(res); // if(StringUtils.isNotBlank(res)){ // // move to processed dir // boolean moved = move(f,new File(toDir+File.separator+f.getName().substring(0,f.getName().indexOf("."))+"_"+res+PNG)); // System.out.println(moved==true?"Moved to processed dir.":"Can't move file: "+f.getAbsolutePath()); // } // } // } // } String res = decode(new File("/mnt/shared/qrcodes/b.png")); // encode("http://baike.baidu.com/link?url=BP7l08ZZZa7HBLL0g4RCuVsN3BlNbuokYRKCuUk8ZexPDjxSVNMXQq_08WI1xz5EUAjVrce4qgSisos9K-mq-q"); } private static boolean move(File f,File toDir){ return f.renameTo(toDir); } /** * 解析图像 */ public static String decode(File file) { BufferedImage image; String res = null; try { image = ImageIO.read(file); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(); // hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码 String resultStr = result.getText(); // System.out.println(resultStr); String t = resultStr.substring(resultStr.indexOf("?")+3,resultStr.length()); String base64 = new String(Base64.decodeBase64(t)); // System.out.println(t); System.out.print(base64+" ---> "); DecodeResult decodeResult = new Gson().fromJson(base64,DecodeResult.class); res = decodeResult.getD(); } catch (Exception e) { // move to processed dir boolean moved = move(file,new File(toDir+File.separator+file.getName().substring(0,file.getName().indexOf("."))+(res==null?"":"_"+res)+PNG)); System.out.println(moved==true?"Exception happened and moved to processed dir.":"Can't move file: "+file.getAbsolutePath()); // e.printStackTrace(); } return res; } public static void snapshot(){ String fileName = new DateTime().toString("yyyyMMddHHmmss"); Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); double h = screensize.getHeight(); double w = screensize.getWidth(); try { Robot robot = new Robot(); // BufferedImage bi=robot.createScreenCapture(new Rectangle(Double.valueOf((w-PNG_SIZE_WIDTH)/2).intValue(),Double.valueOf((h-PNG_SIZE_HEIGHT)/2).intValue(),PNG_SIZE_WIDTH,PNG_SIZE_HEIGHT)); // 根据指定的区域抓取屏幕 BufferedImage bi=robot.createScreenCapture(new Rectangle(screensize)); // 根据指定的区域抓取屏幕 ImageIO.write(bi, "png", new File(fromDir+File.separator+fileName+PNG)); } catch (AWTException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void encode(String content){ int width = 400; // 图像宽度 int height = 400; // 图像高度 String format = "png";// 图像类型 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); try{ BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵 Path path = FileSystems.getDefault().getPath(toDir, "code.png"); MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像 }catch(Exception e){ e.printStackTrace(); } } } class DecodeResult{ private String a; private String b; private String c; private String d; private String v; public String getA() { return a; } public void setA(String a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } public String getC() { return c; } public void setC(String c) { this.c = c; } public String getD() { return d; } public void setD(String d) { this.d = d; } public String getV() { return v; } public void setV(String v) { this.v = v; } }