Code that only displays the built-in camera in Processing

Rather than Processing, it's no longer just java

After posting, I noticed that the contents of Mat will be negative, so I should have used an int array instead of a byte array ... Mat.get () is overloaded ...


import org.opencv.core.*;
import org.opencv.highgui.*;

VideoCapture capture;

void setup(){  
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  capture = new VideoCapture(0);
  fullScreen();
}

void draw(){
  background(0);
   if (capture.isOpened()) {
      Mat video = new Mat();
      capture.read(video);
      
      byte [] b;
      b = new byte[(int)video.size().height * (int)video.size().width*3];
      
      video.get(0,0,b);
      PImage img = createImage((int)video.size().width,(int)video.size().height,ARGB);
      
      for(int i=0;i<(int)video.size().width;i++){
        for(int j=0;j<(int)video.size().height;j++){
          int p = (i+j*(int)video.size().width)*3;
          img.set(i,j,color(fix(b[p+2]),fix(b[p+1]),fix(b[p+0])));
        }
      }
      image(img, 0, 0, (int)video.size().width, (int)video.size().height);
    } 
}

int fix(int val){
  if(val>-1)return val;
  else{
    return 256+val;
  }
}

reference -Basic processing of cv :: MatUsing OpenCV with Qiita Processing (continued)

Recommended Posts

Code that only displays the built-in camera in Processing
Shortcut keys that complement standard output processing in the IDE
Guess the character code in Java
Sample code that uses the Mustache template engine in Spring Boot
Sample code that uses the Mustache template engine JMustache in Java
Order of processing in the program
How to debug the processing in the Ruby on Rails model only on the console
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions
The application absorbs the difference in character code
[HTTP] Status code included in the HTTP response
Code that deletes all files of the specified prefix in AWS S3 (Java)
A description that only the poster can access
Java Error Handling Basics-The story that catch is only picked up in the foreground