001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.cscore; 006 007import edu.wpi.first.util.RuntimeLoader; 008import java.io.IOException; 009import java.util.concurrent.atomic.AtomicBoolean; 010import org.opencv.core.Core; 011 012/** CameraServer CV JNI. */ 013public class CameraServerCvJNI { 014 static boolean libraryLoaded = false; 015 016 static RuntimeLoader<Core> loader = null; 017 018 /** Sets whether JNI should be loaded in the static block. */ 019 public static class Helper { 020 private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); 021 022 /** 023 * Returns true if the JNI should be loaded in the static block. 024 * 025 * @return True if the JNI should be loaded in the static block. 026 */ 027 public static boolean getExtractOnStaticLoad() { 028 return extractOnStaticLoad.get(); 029 } 030 031 /** 032 * Sets whether the JNI should be loaded in the static block. 033 * 034 * @param load Whether the JNI should be loaded in the static block. 035 */ 036 public static void setExtractOnStaticLoad(boolean load) { 037 extractOnStaticLoad.set(load); 038 } 039 040 /** Utility class. */ 041 private Helper() {} 042 } 043 044 static { 045 String opencvName = Core.NATIVE_LIBRARY_NAME; 046 if (Helper.getExtractOnStaticLoad()) { 047 try { 048 CameraServerJNI.forceLoad(); 049 loader = 050 new RuntimeLoader<>(opencvName, RuntimeLoader.getDefaultExtractionRoot(), Core.class); 051 loader.loadLibraryHashed(); 052 } catch (IOException ex) { 053 ex.printStackTrace(); 054 System.exit(1); 055 } 056 libraryLoaded = true; 057 } 058 } 059 060 /** 061 * Force load the library. 062 * 063 * @throws IOException if library load failed 064 */ 065 public static synchronized void forceLoad() throws IOException { 066 if (libraryLoaded) { 067 return; 068 } 069 CameraServerJNI.forceLoad(); 070 loader = 071 new RuntimeLoader<>( 072 Core.NATIVE_LIBRARY_NAME, RuntimeLoader.getDefaultExtractionRoot(), Core.class); 073 loader.loadLibrary(); 074 libraryLoaded = true; 075 } 076 077 /** 078 * Creates a CV source. 079 * 080 * @param name Name. 081 * @param pixelFormat OpenCV pixel format. 082 * @param width Image width. 083 * @param height Image height. 084 * @param fps Frames per second. 085 * @return CV source. 086 */ 087 public static native int createCvSource( 088 String name, int pixelFormat, int width, int height, int fps); 089 090 /** 091 * Put source frame. 092 * 093 * @param source Source handle. 094 * @param imageNativeObj Image native object handle. 095 */ 096 public static native void putSourceFrame(int source, long imageNativeObj); 097 098 /** 099 * Creates a CV sink. 100 * 101 * @param name Name. 102 * @param pixelFormat OpenCV pixel format. 103 * @return CV sink handle. 104 */ 105 public static native int createCvSink(String name, int pixelFormat); 106 107 // /** 108 // * Creates a CV sink callback. 109 // * 110 // * @param name Name. 111 // * @param processFrame Process frame callback. 112 // */ 113 // public static native int createCvSinkCallback(String name, 114 // void (*processFrame)(long time)); 115 116 /** 117 * Returns sink frame handle. 118 * 119 * @param sink Sink handle. 120 * @param imageNativeObj Image native object handle. 121 * @return Sink frame handle. 122 */ 123 public static native long grabSinkFrame(int sink, long imageNativeObj); 124 125 /** 126 * Returns sink frame timeout in microseconds. 127 * 128 * @param sink Sink handle. 129 * @param imageNativeObj Image native object handle. 130 * @param timeout Timeout in seconds. 131 * @return Sink frame timeout in microseconds. 132 */ 133 public static native long grabSinkFrameTimeout(int sink, long imageNativeObj, double timeout); 134 135 /** Utility class. */ 136 private CameraServerCvJNI() {} 137}