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;
        }
  • 相关阅读:
    ssh整合
    自定义Java集合
    java图形界面写个小桌面,内置简单小软件
    java简单日历
    javaSwing
    javaScript封装
    java解析xml文件
    缺省适配器
    适配器模式
    自定义SWT控件一之自定义单选下拉框
  • 原文地址:https://www.cnblogs.com/eason-d/p/11392966.html
Copyright © 2011-2022 走看看