Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Black image captured instead of view from camera #285

Closed
tiwarigaurav2044 opened this issue Dec 16, 2014 · 11 comments
Closed

Black image captured instead of view from camera #285

tiwarigaurav2044 opened this issue Dec 16, 2014 · 11 comments
Labels

Comments

@tiwarigaurav2044
Copy link

hey, i am using windows 64 bit and i am trying a simple capture image code but the it unable to capture an image. the captured image is attached
test

@sarxos
Copy link
Owner

sarxos commented Dec 17, 2014

Hi @tiwarigaurav2044,

It would be more helpful if you post more details, such as:

  • source code,
  • which Webcam Capture API version you are using,
  • what is the camera (the model, vendor, etc),
  • is there a problem with capture from e.g. Skype or other application?

@tiwarigaurav2044
Copy link
Author

this is the source code:

Webcam.setAutoOpenMode(true);
Webcam webcam = Webcam.getDefault();
BufferedImage im = webcam.getImage();
ImageIO.write(im, "PNG", new File("D:\\test.png"));
webcam.close();

the version im using is 0.3.10
the webcam is inbuilt in my laptop
the webcam is fine if i use jframe for live video then it is working fine but its not able to capture an image

@sarxos
Copy link
Owner

sarxos commented Dec 18, 2014

@tiwarigaurav2044,

That's weird, however I never expect anything else from Windows :)

I suppose that this may be caused by the fact that camera buffer is empty at the very beginning. The Webcam Capture API automatically discard the first three frames that comes from the grabber (the buffer memory). This is happening in WebcamDefaultDevice.java line 360. I empirically verified that the camera buffer size is 5, but in all my experiments only the first frame was blank, so it's very weird why you're getting empty image (the 4'th frame from buffer).

This is in regards to the root cause, but later today I will provide you with the source code I would like you to execute to verify what is the number of first non-blank image you receive from camera. By knowing this we can fine-tune the number of frames that must be discarded and I can hopefully do small modification in the code to fix your problem.

@tiwarigaurav2044
Copy link
Author

i got it thanks the first 2 images are blank but the 3rd image is fine. thanks a lot

@sarxos
Copy link
Owner

sarxos commented Dec 19, 2014

Ok, so it seems like all the 5 elements of initial camera buffer were empty. I will introduce the fix to discard all of them before the camera device is open.

@sarxos sarxos added the bug label Dec 19, 2014
@tiwarigaurav2044
Copy link
Author

Is it possible to stream the live video feed onto any another device such as a smart phone through socket programing?

@sarxos
Copy link
Owner

sarxos commented Dec 19, 2014

Yes, this is possible. There is a video streaming proof-of-concent example that demonstrates how to transcode BuffereImage objects that comes from camera device into h.264 stream and access such stream remotely, but please bear in mind that Webcam Capture API has been designed to capture image from camera in the first place, and all the additional functionalities (like transcoding, streaming, etc) must be done with the 3'rd party frameworks.

@tiwarigaurav2044
Copy link
Author

thanks i will look into it.

@sarxos
Copy link
Owner

sarxos commented Dec 24, 2014

I'm opening this ticket so I remember to fix that.

@sarxos sarxos reopened this Dec 24, 2014
@sarxos sarxos changed the title unable to get a snapshot from webcam Black image captured instead of view from camera Dec 27, 2014
@sarxos sarxos closed this as completed in 1a7198a Dec 27, 2014
@sarxos
Copy link
Owner

sarxos commented Dec 27, 2014

@tiwarigaurav2044,

I implemented the change we were talking about. It should work fine now and all of the initial memory buffer elements should be removed so the black image should not appear any more. It is available in 0.3.11-SNAPSHOT.

Just FYI if you want to take a look, the @ryancheu fork of webcam-capture-live-streaming example is adding supports for additional audio streaming (source code at webcam-capture-live-streaming @ ryancheu).

@lenikhilsingh
Copy link

lenikhilsingh commented Apr 3, 2018

Hey iam using windows 10 64 bit ,i have two webcam 1.) inbuild laptop webcam 2.) usb webcam.
my buffered image is fetching black images from my usb webcam, but the same code is working fine with laptop inbuild webcam. Here's my code @sarxos
`

public class WebCamController implements Initializable {
Webcam webcam;
Preferences preference= Preferences.getPreferences();
boolean isRunning = false;

@FXML
private ImageView imageHolder;
@FXML
private Label displayDir;
@FXML
private Button button;
@FXML
private JFXTextField id1;
AlertMaker alert;



@Override
public void initialize(URL url, ResourceBundle rb) {
    webcam = Webcam.getWebcams().get(0);
    webcam.setViewSize(new Dimension(320,240));
    webcam.open();
    new VideoFeedTaker().start();
    final Tooltip buttonTooltip =new Tooltip();
    buttonTooltip.setText("Capture Image");
    button.setTooltip(buttonTooltip);
    
}  
public void setText(String recID){
    this.id1.setText(recID);
    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+id1.getText());
}


class VideoFeedTaker extends Thread {
    
    @Override
    public void run(){
        while(!isRunning){
            BufferedImage image = webcam.getImage();
             if(image == null){
                 break;
             }else{
                WritableImage image1 = SwingFXUtils.toFXImage(image, null);
                imageHolder.setImage(image1);
             }
            try {
                Thread.sleep(42);
            } catch (InterruptedException ex) {
                Logger.getLogger(WebCamController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

@FXML
private void captureImage(ActionEvent event) throws IOException {
    BufferedImage image = webcam.getImage();
    if(image == null){
        System.out.println("this image is null please");
    }else{
        WritableImage image1 = SwingFXUtils.toFXImage(image, null);
        imageHolder.setImage(image1);
    }
    String path = System.getProperty("user.home")+"\\Documents\\camImage\\"+id1.getText()+".jpeg";
    File outputFile = new File(path);
    outputFile.getParentFile().mkdirs();
    ImageIO.write(image, "JPEG", outputFile);
    System.out.println(System.getProperty("user.home"));
    displayDir.setText(path);
    
    if(!isRunning){
        isRunning = true;
        new VideoFeedTaker().start();
    }else{
        isRunning = false;
    }
}

}`

Using javafx and java 8 (IDE netbeans 8.2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants