Java 带有Sarxos运动检测的If语句中的变量未更改

Java 带有Sarxos运动检测的If语句中的变量未更改,java,Java,我试图让我的程序在检测到运动时打印一些“有趣”的图像。一切都是自行工作的,但当我将if(detection==true)语句放入程序时,变量不会更新。如何让它在motionDetected函数内部更新 我曾尝试对变量使用boolean和int,但我认为这可能与我构建它的方式有关 package printstuff; import java.io.File; import java.io.IOException; import java.util.logging.Level; import

我试图让我的程序在检测到运动时打印一些“有趣”的图像。一切都是自行工作的,但当我将if(detection==true)语句放入程序时,变量不会更新。如何让它在motionDetected函数内部更新

我曾尝试对变量使用boolean和int,但我认为这可能与我构建它的方式有关

package printstuff;



import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.util.function.Function;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import java.util.concurrent.TimeUnit;


import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamMotionEvent;
import com.github.sarxos.webcam.WebcamMotionListener;
/**
 * Detect motion.
 * 
 * @author Bartosz Firyn (SarXos)
 */
public class DetectMotion implements WebcamMotionListener {

    public static boolean detection = false;

    public DetectMotion() {

        //creates a webcam motion detector

        WebcamMotionDetector detector = new WebcamMotionDetector(Webcam.getDefault());
        detector.setInterval(100); // one check per 100 ms
        detector.addMotionListener(this);
        detector.start();

    }

    @Override
    public void motionDetected(WebcamMotionEvent wme) {
        //detects motion and should change the detection variable
        System.out.println("Detected motion");
        detection = true;

    }

    public static void main(String[] args) throws IOException, Exception {
        new DetectMotion();
        System.in.read(); // keep program open

        final String[] catImages = new String[4];
        //all the images
        catImages[0] = "https://i.ytimg.com/vi/3v79CLLhoyE/maxresdefault.jpg";
        catImages[1] = "https://i.imgur.com/RFS6RUv.jpg";
        catImages[2] = "https://kiwifarms.net/attachments/dozgry5w4ae3cut-jpg.618924/";
        catImages[3] = "https://www.foodiecrush.com/wp-content/uploads/2017/10/Instant-Pot-Macaroni-and-Cheese-foodiecrush.com-019.jpg";        
        //statement does not work..
        if(detection == true)
        {
        for(int i=0; i<4; i++)
        {

        //saves the picture inside of the array to image.jpg
        String imageUrl = catImages[i];
        String destinationFile = "image.jpg";

        saveImage(imageUrl, destinationFile);

        //sends print job to printer

         PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(new Copies(1));
            PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
            if (pss.length == 0)
              throw new RuntimeException("No printer services available.");
            PrintService ps = pss[0];
            System.out.println("Printing to " + ps);
            DocPrintJob job = ps.createPrintJob();
            FileInputStream fin = new FileInputStream("image.jpg");
            Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
            job.print(doc, pras);
            fin.close();

            java.util.concurrent.TimeUnit.SECONDS.sleep(15);

        }
    }
    }   
public static void saveImage(String imageUrl, String destinationFile) throws IOException {
    //converts url into image
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

}
包装印刷品;
导入java.io.File;
导入java.io.IOException;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入java.util.function.function;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.net.URL;
导入java.util.concurrent.TimeUnit;
导入javax.print.Doc;
导入javax.print.DocFlavor;
导入javax.print.DocPrintJob;
导入javax.print.PrintException;
导入javax.print.PrintService;
导入javax.print.PrintServiceLookup;
导入javax.print.SimpleDoc;
导入javax.print.attribute.HashPrintRequestAttributeSet;
导入javax.print.attribute.PrintRequestAttributeSet;
导入javax.print.attribute.standard.Copies;
导入com.github.sarxos.webcam.webcam;
导入com.github.sarxos.webcam.WebcamMotionDetector;
导入com.github.sarxos.webcam.WebcamMotionEvent;
导入com.github.sarxos.webcam.WebcamMotionListener;
/**
*检测运动。
* 
*@作者Bartosz Firyn(SarXos)
*/
公共类DetectMotion实现WebCammontionListener{
公共静态布尔检测=false;
公共检测运动(){
//创建网络摄像头运动检测器
WebcamMotionDetector=新的WebcamMotionDetector(Webcam.getDefault());
detector.setInterval(100);//每100毫秒检查一次
detector.addMotionListener(this);
检测器。启动();
}
@凌驾
检测到公共视频(WebCammontionEvent wme){
//检测运动并应更改检测变量
System.out.println(“检测到的运动”);
检测=真;
}
公共静态void main(字符串[]args)引发IOException,异常{
新检测运动();
System.in.read();//保持程序打开
最终字符串[]catImages=新字符串[4];
//所有图像
catImages[0]=”https://i.ytimg.com/vi/3v79CLLhoyE/maxresdefault.jpg";
catImages[1]=”https://i.imgur.com/RFS6RUv.jpg";
catImages[2]=”https://kiwifarms.net/attachments/dozgry5w4ae3cut-jpg.618924/";
catImages[3]=”https://www.foodiecrush.com/wp-content/uploads/2017/10/Instant-Pot-Macaroni-and-Cheese-foodiecrush.com-019.jpg";        
//语句不起作用。。
如果(检测==真)
{

对于(int i=0;如果从外观上看,我猜
检测
是由另一个线程设置的。您是否尝试将其声明为
volatile
?您的
main
方法在
System.in.read()
。键入某个内容后,它将继续运行。也许您应该将
if(detection==true){…}
转换为
while(true)
loop@Turing85我在declare语句中添加了“volatile”,它在“motionDetected”函数中打印为true,但它仍然会导致if语句工作。@DawoodibnKareem键入的内容确实破坏了它。是否有自动执行此操作的方法?从外观上看,我猜
检测
是由另一个设置的线程。您是否尝试将其声明为
volatile
?您的
main
方法在
System.in.read()
。键入内容后,它将继续。也许您应该将
if(detection==true){…}
放入
while(true)中
loop@Turing85我在declare语句中添加了“volatile”,它在“motionDetected”函数中打印为true,但它仍然会导致if语句工作。@DawoodibnKareem键入的内容确实破坏了它。是否自动执行该操作?