简体   繁体   中英

Java saving large images in threads - IP camera broken images

After clicking a button in my app, it creates new normal Thread and start downloading large image and saving it to file. Everything is going well, but when i click button more than once it's going without errors and when i try to view these images they're bugged like they re overwriting themself.

I don't have any idea how to debug it.

localPath = today + "/" + productCode + "/" + this.placeId; //Unique

/* ... */

private void productSave(String productCode, int whichCamera, boolean isError) {
  for (int i = position; i < lastCamera; i++) {
    Date dateSave = new Date();
    path = localPath + "/" + dateFormat.format(dateSave) + "_" + (i + 1) + ".jpg";

    try {
      BufferedImage imageOld = ImageIO.read(new URL(this.camerasUrlsToSave[i]));

      ImageIO.write(imageOld, "jpg", new File(rootPath + "/" + path));

      ComDb.getInstance().saveProduct(productCode, this.placeId, path, dateSave);
    } catch (IOException ex) {
      result = false;
    }
  }
}

EDIT: path is 100% unique (different folders with product code). And it shoudn't be problem with image from camera - I can open 10 cards i dont see image bugs

EDIT2: Can it be something like downloading Img bufor? Cause all images are downloaded from the same IP. Or maybe its problem with bufferedimg memory leaks. Need idea how to repair it.

EDIT3: I found that if i open 5 cards in web browser with my camera address like : blah.blah.some.ip/GetImage.cgi?CH=0 They're loading one after the other, not all at once. But, i dont see bugged images when downloading ends.

EDIT4: I tried to reproduce this bug in web browser, if i try to open link in ff and in IE. IE prints "getImage busy". When I try ff and chrome i got broken images. So i have to do sth like queue or disable button ...

EDIT5: My temporary solution: synchronized function productSave. Images from second click will be saved few seconds later.

http://oi57.tinypic.com/ofrrn.jpg !

One from saved Images

First action of the click event for the button should be to disable the button and maybe change the text to "In process". Last action should be to re-enable the button and restore the text.

This answer is a guess as you have not given full code, issue could be with variable i - where is it from?

Or could be same file name is being reused, mnake sure that is nto the case by getting unique file name from a seperate function something like this:

if dateFormat is only to minute or second, same file name might be used for 2 images use this API of java.io.File to get a unique name

http://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String,%20java.io.File%29

public static File createTempFile(String prefix, String suffix, File directory)

//you can pass extn as jpg

public File getFileName(File localPath ,Date dateSave, int i, String extn){
    File fileUniqe = File.createTempFile(dateSave + "_" + (i+1), extn, localPath );
    return fileUniqe,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM