• 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);
            
            // Read from a URL(topit.me网站上随便找了一张图)
            URL url = new URL("http://www.topit.cc/uploads/20180303/17/1520068648-XwNkvKgBVP.jpg");
            image = ImageIO.read(url);
            f = new File("topit.jpg");
            ImageIO.write(image, "jpg", f);
        } catch (IOException e) {
        }
       }
    }

     RGB转灰度

    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);
           
            
            int width=image.getWidth();  
        int height=image.getHeight();  
        int imaRGB[][]=new int[width][height];//存放RGB信息数组,重点  
            int grey[][]=new int[width][height];//存放RGB信息数组,重点  
        //从bufIma读取RGB到数组中  
        int minx = image.getMinX();
            int miny = image.getMinY();
            int[] rgb = new int[3];
            for (int i = minx; i < width; i++) {
                for (int j = miny; j < height; j++) {
                    int pixel = image.getRGB(i, j);//获得像素值
                    rgb[0] = (pixel & 0xff0000) >> 16;
                    rgb[1] = (pixel & 0xff00) >> 8;
                    rgb[2] = (pixel & 0xff);
                    //此处为将像素值转换为灰度值的方法,存在误差,算法不唯一
                    grey[i][j] = (rgb[0]*150+rgb[1]*59+rgb[2]*11+150)/150;  
                }
            }
             
            for(int i=0;i<width;i++){ 
              System.out.print("
    "); 
              for(int j=0;j<height;j++)  
                 System.out.print(" "+grey[i][j]);  
            }         
            System.out.print("image size: "+width+" , "+height);  
            System.out.print("minx: "+width+" , miny: "+height);  
        } catch (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里的函数名写错了。

  • 相关阅读:
    @RequestParam 加与不加的区别
    spring boot 实战
    mongo入门
    npm install 错误记录
    AsyncConfigurer 线程池
    guava Preconditions
    mysql分组、合并语句
    maven的学习以及集成开发软件
    Spring MVC+Junit测试出错---@WebAppConfiguration
    mybatis的代码生成器
  • 原文地址:https://www.cnblogs.com/zhengmeisong/p/8535468.html
Copyright © 2020-2023  润新知