public void setupCamera(int width, int height) {
    Log.i(TAG, "setupCamera(" + width + ", " + height + ")");
    synchronized (this) {
      if (mCamera != null && mCamera.isOpened()) {
        List<Size> sizes = mCamera.getSupportedPreviewSizes();
        int mFrameWidth = width;
        int mFrameHeight = height;

        // selecting optimal camera preview size
        {
          double minDiff = Double.MAX_VALUE;
          for (Size size : sizes) {
            if (Math.abs(size.height - height) < minDiff) {
              mFrameWidth = (int) size.width;
              mFrameHeight = (int) size.height;
              minDiff = Math.abs(size.height - height);
            }
          }
        }

        mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
        mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
      }
    }
  }
  public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
    Log.i(TAG, "surfaceCreated");
    synchronized (this) {
      if (mCamera != null && mCamera.isOpened()) {
        Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
        List<Size> sizes = mCamera.getSupportedPreviewSizes();
        Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
        int mFrameWidth = width;
        int mFrameHeight = height;

        // selecting optimal camera preview size
        {
          double minDiff = Double.MAX_VALUE;
          for (Size size : sizes) {
            if (Math.abs(size.height - height) < minDiff) {
              mFrameWidth = (int) size.width;
              mFrameHeight = (int) size.height;
              minDiff = Math.abs(size.height - height);
            }
          }
        }

        mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
        mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
      }
    }
  }
Example #3
0
  public static void main(String args[]) {
    // Load the OpenCV library
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    // Setup the camera
    VideoCapture camera = new VideoCapture();
    camera.open(0);

    // Create GUI windows to display camera output and OpenCV output
    int width = 320;
    int height = 240;
    camera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, width);
    camera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, height);
    JLabel cameraPane = createWindow("Camera output", width, height);
    JLabel opencvPane = createWindow("OpenCV output", width, height);

    // Main loop
    Mat rawImage = new Mat();
    while (true) {
      // Wait until the camera has a new frame
      while (!camera.read(rawImage)) {
        try {
          Thread.sleep(1);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      // Process the image however you like
      Mat binary = Detection.detectHueRange(rawImage);
      org.opencv.core.Point center = Detection.nextCenter(binary, width / 2, height / 2, 5);
      Mat processedImage = Detection.convertC(binary);
      System.out.println(center.x);
      System.out.println(center.y);

      //            Mat lines = Detection.detectEdges(rawImage, 80, 3);
      //            List<org.opencv.core.Point> edges = Detection.findWallEdges(lines, rawImage,
      // 25);
      //            Detection.drawLines(binary, edges);
      //            Mat processedImage = Detection.convertC(binary);
      Mat edges = Detection.contourImage(rawImage, 150, 3);
      Detection.hueEdges(rawImage, edges);
      Mat edgesC = Detection.convertC(edges);
      //            List<org.opencv.core.Point> lines = Detection.hueLines(edges);
      //            Detection.drawLines(binary, lines);
      //            Mat processedImage = Detection.convertC(binary);

      // Update the GUI windows
      updateWindow(cameraPane, rawImage);
      //            updateWindow(opencvPane, processedImage);
      updateWindow(opencvPane, edgesC);
    }
  }
Example #4
0
  /** Capture images and run color processing through here */
  public void capture() {
    VideoCapture camera = new VideoCapture();

    camera.set(12, -20); // change contrast, might not be necessary

    // CaptureImage image = new CaptureImage();

    camera.open(0); // Useless
    if (!camera.isOpened()) {
      System.out.println("Camera Error");

      // Determine whether to use System.exit(0) or return

    } else {
      System.out.println("Camera OK");
    }

    boolean success = camera.read(capturedFrame);
    if (success) {
      try {
        processWithContours(capturedFrame, processedFrame);
      } catch (Exception e) {
        System.out.println(e);
      }
      // image.processFrame(capturedFrame, processedFrame);
      // processedFrame should be CV_8UC3

      // image.findCaptured(processedFrame);

      // image.determineKings(capturedFrame);

      int bufferSize = processedFrame.channels() * processedFrame.cols() * processedFrame.rows();
      byte[] b = new byte[bufferSize];

      processedFrame.get(0, 0, b); // get all the pixels
      // This might need to be BufferedImage.TYPE_INT_ARGB
      img =
          new BufferedImage(
              processedFrame.cols(), processedFrame.rows(), BufferedImage.TYPE_INT_RGB);
      int width = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_WIDTH);
      int height = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT);
      // img.getRaster().setDataElements(0, 0, width, height, b);
      byte[] a = new byte[bufferSize];
      System.arraycopy(b, 0, a, 0, bufferSize);

      Highgui.imwrite("camera.jpg", processedFrame);
      System.out.println("Success");
    } else System.out.println("Unable to capture image");

    camera.release();
  }
  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  @Override
  public void run() {
    if (MODE.equals("VIDEO")) {
      Mat capturedImage = new Mat();
      VideoCapture vc = new VideoCapture(DEVICE);
      if (!vc.isOpened()) {
        System.out.println("Capture Failed!");
        return;
      }
      System.out.println("Device " + DEVICE + " opened");
      // set captured resolution
      vc.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 640);
      vc.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 480);

      // Manually set exposure
      vc.set(15, -11);
      while (true) {
        vc.read(capturedImage);
        if (capturedImage != null) {
          // flip the image to compensate for camera orientation
          Core.flip(capturedImage, capturedImage, -1);
          capturedImage.copyTo(finalDisplayImg);
          parseImage(capturedImage);
        }
      }
    } else { // STILL IMAGE
      Mat capturedImage = Highgui.imread(IMAGE_FILEPATH);
      while (true) {
        if (needUpdate) {
          capturedImage.copyTo(finalDisplayImg);
          parseImage(capturedImage);
          needUpdate = false;
        }
      }
    }
  }
  public void setupCamera(int width, int height) {
    Log.i(TAG, "setupCamera out(" + width + ", " + height + ")");
    synchronized (this) {
      if (mCamera != null && mCamera.isOpened()) {
        List<Size> sizes = mCamera.getSupportedPreviewSizes();
        int mFrameWidth = width;
        int mFrameHeight = height;

        // selecting optimal camera preview size
        {
          double minDiff = Double.MAX_VALUE;
          for (Size size : sizes) {
            if ((Math.abs(size.height - height) + Math.abs(size.width - width)) < minDiff) {
              mFrameWidth = (int) size.width;
              mFrameHeight = (int) size.height;
              minDiff = Math.abs(size.height - height);
            }
          }
        }

        mCamera.set(
            Highgui.CV_CAP_PROP_ANDROID_ANTIBANDING, Highgui.CV_CAP_ANDROID_ANTIBANDING_OFF);
        mCamera.set(Highgui.CV_CAP_PROP_ANDROID_FLASH_MODE, Highgui.CV_CAP_ANDROID_FLASH_MODE_OFF);
        mCamera.set(
            Highgui.CV_CAP_PROP_ANDROID_FOCUS_MODE,
            Highgui.CV_CAP_ANDROID_FOCUS_MODE_CONTINUOUS_VIDEO);
        mCamera.set(
            Highgui.CV_CAP_PROP_ANDROID_WHITE_BALANCE,
            Highgui.CV_CAP_ANDROID_WHITE_BALANCE_FLUORESCENT);
        //                mCamera.set(Highgui.CV_CAP_PROP_IOS_DEVICE_EXPOSURE,
        //                Log.i(TAG, "setupCamera 6: " +
        // mCamera.get(Highgui.CV_CAP_PROP_IOS_DEVICE_EXPOSURE));

        mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
        mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
      }
    }
  }