Webcam capture/ possible to crop a PImage

edited November 2016 in Library Questions

My code takes a screenshot of the window; the goal is to take a screenshot of the webcam feed, which is smaller than the program window, which is necessary for what my end goal. It then loads the screenshot into the program as a PImage. Is there a way to only capture the image from the dimensions of the webcam feed? Or is there a way to crop the .jpg in the data folder without leaving the program?

import processing.video.*;

Capture cam;

PImage capture;

void setup() {
  //window size
  size(1280, 720, P3D);
  background (0);
  noCursor();
  //webcam feed
  capture = loadImage ("capture.jpg");
  imageMode (CENTER);

  String[] cameras = Capture.list();

  //webcam feed
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }

    cam = new Capture(this, cameras[0]);
    cam.start();
  }
}

void draw() {
  if (cam.available() == true) {
    cam.read();
  }
  //web cam feed position, size
  image(cam, 640, 360, 320, 180);
}

void mousePressed() {
  //image capture
  saveFrame("data/capture.jpg");
  cam.read();

  //loads image capture in
  image (capture, 200, 200, 480, 270);
  image (capture, 1000, 500, 480, 270);
}

Answers

  • please don't post duplicates

    what's up with the answers on this other thread?

    https://forum.processing.org/two/discussion/19260/resizing-a-captured-image

  • edited November 2016

    It's not a duplicate. Turns out I had the opposite problem, so I made a new post. And the answer to the other didn't help anything at all, just sent me backwards in my work.

  • Thanks Kf

  • Answer ✓

    Ok, so the other posts were closed. I figure your problem why the second screenshot is not being shown. That is because you load your screen capture image in setup. You should do that in mousePressed instead after you save your current frame. Add the following line there and remove the one in setup:

      capture = loadImage ("capture.jpg");
    

    Also, you need to remove cam.read() in mousePressed. Please review the reference to find out why.

    Kf

  • edited November 2016 Answer ✓

    In general, when you have a PImage or PGraphics pixel array...

    If you want to display the image at a different size, use image() to draw it at the new size:

    image( myimage, x, y, width, height);
    

    This will display your image at the new width and height. See the image() reference for details.

    If you want to crop the image to a different size, use get() or copy():

    get( myimage, x, y, width, height);
    

    See the get() reference or the copy() reference for details.

Sign In or Register to comment.