Java Redis JNI
基本参考菜鸟教程,java 下载直接安装,注意文件名和类名需要一致的问题;
redis 下载以后按菜鸟教程linux 下安装,方式编译运行 ok;
Java 使用 redis 按菜鸟教程下载.jar,保存在本地某个路径,然后配 classpath 按这里的第一种方法:
export CLASSPATH=$CLASSPATH:/path/to/file/java-servlet-api.jar source ~/.bash_profile //网站上少了~/
代码来自菜鸟教程:
import redis.clients.jedis.Jedis; public class helloworld{ public static void main(String[] args){ System.out.println("Hello World"); Jedis jedis = new Jedis("localhost"); System.out.println("success"); System.out.println("running:"+jedis.ping());} }
效果:
javaTest song$ java helloworld
Hello World
success
running: PONG
jni 编译路径找不到,mac os 在这里:
/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/include/jni.h
/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/include/darwin/jni_md.h
------------18.03.12 Java 读取图像并保存 ----------
多方参考,这里的代码可以读取,应该有什么依赖环境没装上,所以不能显示,想把读到的图片另存在本地,保存总是报错,后来添加头文件可以了。
import javax.imageio.*;//for ImageIO.read import java.util.Arrays; import java.awt.image.BufferedImage; import java.awt.*;//for JFrame and JLabel etc import javax.swing.*;//for JFrame and JLabel etc import java.net.*;//for URL import java.io.*;//for catch (IOException e),File,InputStream, BufferedInputStream,and FileInputStream ect public class HelloJava{ public static void main (String[] args){ try { // Read from a file File sourceimage = new File("test.jpg"); //source.gif 图片要与 HelloJava.java 同在一目录下 BufferedImage image = ImageIO.read(sourceimage); File f = new File("test1.jpg"); ImageIO.write(image, "jpg", f);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Read from a URL(topit.me网站上随便找了一张图)</span> URL url = <span style="color: rgba(0, 0, 255, 1)">new</span> URL(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://www.topit.cc/uploads/20180303/17/1520068648-XwNkvKgBVP.jpg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); image </span>=<span style="color: rgba(0, 0, 0, 1)"> ImageIO.read(url); f </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> File(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">topit.jpg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); ImageIO.write(image, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">jpg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, f); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { }
}
}
import javax.imageio.*;//for ImageIO.read import java.util.Arrays; import java.awt.image.BufferedImage; import java.awt.*;//for JFrame and JLabel etc import javax.swing.*;//for JFrame and JLabel etc import java.net.*;//for URL import java.io.*;//for catch (IOException e),File,InputStream, BufferedInputStream,and FileInputStream ect public class readRGB2Grey{ public static void main (String[] args){ try { // Read from a file File sourceimage = new File("topit_cut.jpg"); //source.gif 图片要与 HelloJava.java 同在一目录下 BufferedImage image = ImageIO.read(sourceimage);</span><span style="color: rgba(0, 0, 255, 1)">int</span> width=<span style="color: rgba(0, 0, 0, 1)">image.getWidth(); </span><span style="color: rgba(0, 0, 255, 1)">int</span> height=<span style="color: rgba(0, 0, 0, 1)">image.getHeight(); </span><span style="color: rgba(0, 0, 255, 1)">int</span> imaRGB[][]=<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[width][height];<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存放RGB信息数组,重点 </span> <span style="color: rgba(0, 0, 255, 1)">int</span> grey[][]=<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[width][height];<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存放RGB信息数组,重点 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从bufIma读取RGB到数组中 </span> <span style="color: rgba(0, 0, 255, 1)">int</span> minx =<span style="color: rgba(0, 0, 0, 1)"> image.getMinX(); </span><span style="color: rgba(0, 0, 255, 1)">int</span> miny =<span style="color: rgba(0, 0, 0, 1)"> image.getMinY(); </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] rgb = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">]; </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = minx; i < width; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> j = miny; j < height; j++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">int</span> pixel = image.getRGB(i, j);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得像素值</span> rgb[<span style="color: rgba(128, 0, 128, 1)">0</span>] = (pixel & <span style="color: rgba(128, 0, 128, 1)">0xff0000</span>) >> <span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">; rgb[</span><span style="color: rgba(128, 0, 128, 1)">1</span>] = (pixel & <span style="color: rgba(128, 0, 128, 1)">0xff00</span>) >> <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">; rgb[</span><span style="color: rgba(128, 0, 128, 1)">2</span>] = (pixel & <span style="color: rgba(128, 0, 128, 1)">0xff</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">此处为将像素值转换为灰度值的方法,存在误差,算法不唯一</span> grey[i][j] = (rgb[<span style="color: rgba(128, 0, 128, 1)">0</span>]*<span style="color: rgba(128, 0, 128, 1)">150</span>+rgb[<span style="color: rgba(128, 0, 128, 1)">1</span>]*<span style="color: rgba(128, 0, 128, 1)">59</span>+rgb[<span style="color: rgba(128, 0, 128, 1)">2</span>]*<span style="color: rgba(128, 0, 128, 1)">11</span>+<span style="color: rgba(128, 0, 128, 1)">150</span>)/<span style="color: rgba(128, 0, 128, 1)">150</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> i=<span style="color: rgba(128, 0, 128, 1)">0</span>;i<width;i++<span style="color: rgba(0, 0, 0, 1)">){ System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.print(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> j=<span style="color: rgba(128, 0, 128, 1)">0</span>;j<height;j++<span style="color: rgba(0, 0, 0, 1)">) System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.print(<span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">grey[i][j]); } System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.print(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">image size: </span><span style="color: rgba(128, 0, 0, 1)">"</span>+width+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"> , </span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">height); System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.print(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">minx: </span><span style="color: rgba(128, 0, 0, 1)">"</span>+width+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"> , miny: </span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">height); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { }
}
}
------18.03.14 jni---------------------
java 用 JNI 调 c++ 程序包搞了两天半,基本是参考前人程序,步骤:
先编译通过 c++ 程序,测试没问题以后打包成动态链接库,拷贝 c.hpp 到 java 目录;
在 java 工程目录下新建目录:com/test/,进入目录写 java :helloword.java
javac helloword.java,如果没问题会生成 hello.class
回到主目录 javah com.test.helloword,如果没问题会生成 com_test_nativehelloword.h
参考.h 里的定义写 jni_nativehelloword.cpp, 在这里 include<c.hpp> 调 c++ 生成的动态链接库。
注意事项:
如果要返回一个 jobject(ArrayList), jni 里是返回 jobject, return NULL, java 里判断 ArrayList 是否为空用 (arraylist!=null&&.size()>0), 注意大小写。
-------18.03.15 jni-------------------
c++ 代码种新加了一个函数,java jni 全更新过一遍,报错:
Undefined symbols for architecture x86_64: "_queryDB", referenced from: method_table in jni_SearchImage-a0aaca.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
整个检查了一圈,各种怀疑,后来发现是 jni 里的函数名写错了。