Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package piexl2;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.awt.image.BufferedImage;
  10. import javax.imageio.ImageIO;
  11. import java.awt.Graphics2D;
  12. import java.awt.Image;
  13. /**
  14.  *
  15.  * @author Stepan
  16.  */
  17. public class Piexl2 {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.    
  23.     static BufferedImage resize(BufferedImage img, int newW, int newH) {
  24.         Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
  25.         BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
  26.         Graphics2D g2d = dimg.createGraphics();
  27.         g2d.drawImage(tmp, 0, 0, null);
  28.         g2d.dispose();
  29.         return dimg;
  30.     }  
  31.    
  32.     static int get_RGB(BufferedImage img, int x, int y){
  33.         int w = img.getWidth();
  34.         int h = img.getHeight();
  35.         if (x >= w || y >= h)
  36.             return 0;
  37.         return img.getRGB(x, y);
  38.     }
  39.    
  40.     public static int[] full_size(BufferedImage img, int sz){
  41.         int w = img.getWidth();
  42.         int h = img.getHeight();
  43.         int[] arr = new int[2];
  44.         arr[0] = w + w % sz;
  45.         arr[1] = h + h % sz;
  46.         return arr;
  47.     }
  48.     static int abs(int a){
  49.         if (a < 0)
  50.             a = a * -1;
  51.         return a;
  52.     }
  53.     static int get_color(int p){
  54.         int a = (p>>24) & 0xff; ///Alpha
  55.         int r = (p>>16) & 0xff; ///red
  56.         int g = (p>>8) & 0xff; ///green
  57.         int b = p & 0xff; ///blue
  58.         double res = 0.2126 * r + 0.7152 * g + 0.0722 * b; // оттенок черного (> или < 128)
  59.         if (res < 160)
  60.             return 0;
  61.         return 1;
  62.     }
  63.    
  64.     static int[][] build_pixel_array(BufferedImage img, int x, int y){
  65.         int[][] pixels = new int[y][x];
  66.         for (int i = 0; i < y; i++){
  67.             for (int j = 0; j < x; j++){
  68.                 pixels[i][j] = get_color(get_RGB(img, j, i));
  69.                 //System.out.print(pixels[i][j]);
  70.             }
  71.             //System.out.print('\n');
  72.         }
  73.         return pixels;
  74.     }
  75.    
  76.     static int[][] build_final_array(BufferedImage img, int[][] pixels, int a, int b, int sz){
  77.         int[][] final_ = new int[b / sz][a / sz];
  78.         for (int j = 0; j < b; j = j + sz){
  79.             for (int i = 0; i < a; i = i + sz){
  80.                 int rgb = 0;
  81.                 for (int y1 = 0; y1 < sz; y1++){
  82.                     for (int x1 = 0; x1 < sz; x1++){
  83.                         rgb += pixels[j + y1][i + x1];
  84.                         //abs(get_RGB(img, i + x1, j + y1));
  85.                     }
  86.                 }
  87.                 //rgb = rgb / (sz * sz);
  88.                 System.out.print(rgb + "\n");
  89.                 //System.out.print(i / sz + " " + j / sz + "\n");
  90.                 //System.out.print(get_color(rgb));
  91.                 int ans = -1;
  92.                 if (rgb < sz * sz / 2)
  93.                     ans = 0;
  94.                 else
  95.                     ans = 1;
  96.                 final_[j / sz][i / sz] = ans;
  97.                 System.out.print(final_[j / sz][i / sz] + "\n");
  98.                
  99.             }
  100.         }
  101.         return final_;
  102.     }
  103.    
  104.     public static void main(String[] args) {
  105.        
  106.         //  reading image
  107.         int X = 0, Y = 0;
  108.         int[][] final1;
  109.         BufferedImage img = null, newimg = null;
  110.         File f = null;
  111.         try{
  112.             String adress_from = "c:\\stepa\\Android_projects\\Piexl2\\src\\piexl2\\Eminem.jpg";
  113.             String adress_to = "c:\\stepa\\Android_projects\\Piexl2\\src\\piexl2\\Eminem1.jpg";
  114.             f = new File(adress_from);
  115.             img = ImageIO.read(f);
  116.             int x = img.getWidth();
  117.             int y = img.getHeight();
  118.             newimg = resize(img, x, (int) (y / 2.8) + 1);
  119.             X = newimg.getWidth();
  120.             Y = newimg.getHeight();
  121.            
  122.         }catch(IOException e){
  123.             System.out.println("Error while reading image");
  124.         }
  125.        
  126.         int sz = 5;
  127.         int[] arr = full_size(newimg, sz);
  128.         int[][] pixels = build_pixel_array(newimg, arr[0], arr[1]);
  129.         final1 = build_final_array(newimg, pixels, arr[0], arr[1], sz);
  130.        
  131.         //printnig
  132.         System.out.print(arr[0] / sz + " " + arr[1] / sz + "\n");
  133.         for (int i = 0; i < arr[1] / sz; i++){
  134.             for (int j = 0; j < arr[0] / sz; j++){
  135.                 System.out.print(final1[i][j] + "");
  136.             }
  137.             System.out.print("\n");
  138.         }
  139.         System.out.print("\n\n\n\n" + X + ", " + Y + "\n");
  140.        
  141.         for (int j = 0; j < Y - 1; j++){
  142.             for (int i = 0; i < X - 1; i++){
  143.                 System.out.print(pixels[j][i] + "");
  144.             }
  145.             System.out.print("\n");
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement