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/** OpenCV Native Loader. */ 013public final class OpenCvLoader { 014 @SuppressWarnings("PMD.MutableStaticState") 015 static boolean libraryLoaded; 016 017 /** Sets whether JNI should be loaded in the static block. */ 018 public static final class Helper { 019 private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); 020 021 /** 022 * Returns true if the JNI should be loaded in the static block. 023 * 024 * @return True if the JNI should be loaded in the static block. 025 */ 026 public static boolean getExtractOnStaticLoad() { 027 return extractOnStaticLoad.get(); 028 } 029 030 /** 031 * Sets whether the JNI should be loaded in the static block. 032 * 033 * @param load Whether the JNI should be loaded in the static block. 034 */ 035 public static void setExtractOnStaticLoad(boolean load) { 036 extractOnStaticLoad.set(load); 037 } 038 039 /** Utility class. */ 040 private Helper() {} 041 } 042 043 static { 044 if (Helper.getExtractOnStaticLoad()) { 045 try { 046 RuntimeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME); 047 } catch (IOException ex) { 048 ex.printStackTrace(); 049 try { 050 // Try adding a debug postfix 051 RuntimeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME + "d"); 052 } catch (IOException e) { 053 e.printStackTrace(); 054 System.exit(1); 055 } 056 } 057 libraryLoaded = true; 058 } 059 } 060 061 /** 062 * Forces a static load. 063 * 064 * @return a garbage value 065 */ 066 public static int forceStaticLoad() { 067 return libraryLoaded ? 1 : 0; 068 } 069 070 /** 071 * Force load the library. 072 * 073 * @throws IOException if library load failed 074 */ 075 public static synchronized void forceLoad() throws IOException { 076 if (libraryLoaded) { 077 return; 078 } 079 try { 080 RuntimeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME); 081 } catch (IOException e) { 082 e.printStackTrace(); 083 // Try adding a debug postfix 084 RuntimeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME + "d"); 085 } 086 libraryLoaded = true; 087 } 088 089 /** Utility class. */ 090 private OpenCvLoader() {} 091}