zoukankan      html  css  js  c++  java
  • Java BufferImage 获取像素矩阵 或 数组

    参考地址:

    https://blog.csdn.net/u013248535/article/details/53929605/

       private static int WHITE = new Color(255, 255, 255).getRGB();

        private static int BLACK = new Color(0, 0, 0).getRGB();

      /**
         * 文件转 int[]数组
         * @param file
         * @return
         */
        public static int[] fileToIntArray(File file){
            try {
                BufferedImage image = ImageIO.read(file);
                int width = image.getWidth();
                int height = image.getHeight();
                return bufferedImageToIntArray(image, width, height);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * BufferedImage转 int[]数组
         * @param file
         * @return
         */
        public static int[] bufferedImageToIntArray(BufferedImage image, int width, int height){
            try {
                int rgb = 0;
                int[] data = new int[width * height];
                // 方式一:通过getRGB()方式获得像素数组
                // 此方式为沿Height方向扫描
                for (int i = 0; i < width; i++) {
                    for (int j = 0; j < height; j++) {
                        rgb = image.getRGB(i, j);
                        if(rgb == -1){
                            //白色
                            data[i + j * width] = WHITE;
                        } else {
                            //黑色
                            data[i + j * width] = BLACK;
                        }
                        //System.out.print(data[i + j * width]+" ");
                    }
                    //System.out.println();
                }
                return data;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
  • 相关阅读:
    菜单导航组件
    flask+nginx+uwsgi在服务器搭建项目
    安装Anaconda
    vscode上eslink包失效
    js滚动事件
    打字游戏
    js更高文档的样式
    js事件
    Dom对象更改文档结构.html
    js重点
  • 原文地址:https://www.cnblogs.com/eason-d/p/11392966.html
Copyright © 2011-2022 走看看