Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package piexl2;
- import java.io.File;
- import java.io.IOException;
- import java.awt.image.BufferedImage;
- import javax.imageio.ImageIO;
- import java.awt.Graphics2D;
- import java.awt.Image;
- /**
- *
- * @author Stepan
- */
- public class Piexl2 {
- /**
- * @param args the command line arguments
- */
- static BufferedImage resize(BufferedImage img, int newW, int newH) {
- Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
- BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2d = dimg.createGraphics();
- g2d.drawImage(tmp, 0, 0, null);
- g2d.dispose();
- return dimg;
- }
- static int get_RGB(BufferedImage img, int x, int y){
- int w = img.getWidth();
- int h = img.getHeight();
- if (x >= w || y >= h)
- return 0;
- return img.getRGB(x, y);
- }
- public static int[] full_size(BufferedImage img, int sz){
- int w = img.getWidth();
- int h = img.getHeight();
- int[] arr = new int[2];
- arr[0] = w + w % sz;
- arr[1] = h + h % sz;
- return arr;
- }
- static int abs(int a){
- if (a < 0)
- a = a * -1;
- return a;
- }
- static int get_color(int p){
- int a = (p>>24) & 0xff; ///Alpha
- int r = (p>>16) & 0xff; ///red
- int g = (p>>8) & 0xff; ///green
- int b = p & 0xff; ///blue
- double res = 0.2126 * r + 0.7152 * g + 0.0722 * b; // оттенок черного (> или < 128)
- if (res < 160)
- return 0;
- return 1;
- }
- static int[][] build_pixel_array(BufferedImage img, int x, int y){
- int[][] pixels = new int[y][x];
- for (int i = 0; i < y; i++){
- for (int j = 0; j < x; j++){
- pixels[i][j] = get_color(get_RGB(img, j, i));
- //System.out.print(pixels[i][j]);
- }
- //System.out.print('\n');
- }
- return pixels;
- }
- static int[][] build_final_array(BufferedImage img, int[][] pixels, int a, int b, int sz){
- int[][] final_ = new int[b / sz][a / sz];
- for (int j = 0; j < b; j = j + sz){
- for (int i = 0; i < a; i = i + sz){
- int rgb = 0;
- for (int y1 = 0; y1 < sz; y1++){
- for (int x1 = 0; x1 < sz; x1++){
- rgb += pixels[j + y1][i + x1];
- //abs(get_RGB(img, i + x1, j + y1));
- }
- }
- //rgb = rgb / (sz * sz);
- System.out.print(rgb + "\n");
- //System.out.print(i / sz + " " + j / sz + "\n");
- //System.out.print(get_color(rgb));
- int ans = -1;
- if (rgb < sz * sz / 2)
- ans = 0;
- else
- ans = 1;
- final_[j / sz][i / sz] = ans;
- System.out.print(final_[j / sz][i / sz] + "\n");
- }
- }
- return final_;
- }
- public static void main(String[] args) {
- // reading image
- int X = 0, Y = 0;
- int[][] final1;
- BufferedImage img = null, newimg = null;
- File f = null;
- try{
- String adress_from = "c:\\stepa\\Android_projects\\Piexl2\\src\\piexl2\\Eminem.jpg";
- String adress_to = "c:\\stepa\\Android_projects\\Piexl2\\src\\piexl2\\Eminem1.jpg";
- f = new File(adress_from);
- img = ImageIO.read(f);
- int x = img.getWidth();
- int y = img.getHeight();
- newimg = resize(img, x, (int) (y / 2.8) + 1);
- X = newimg.getWidth();
- Y = newimg.getHeight();
- }catch(IOException e){
- System.out.println("Error while reading image");
- }
- int sz = 5;
- int[] arr = full_size(newimg, sz);
- int[][] pixels = build_pixel_array(newimg, arr[0], arr[1]);
- final1 = build_final_array(newimg, pixels, arr[0], arr[1], sz);
- //printnig
- System.out.print(arr[0] / sz + " " + arr[1] / sz + "\n");
- for (int i = 0; i < arr[1] / sz; i++){
- for (int j = 0; j < arr[0] / sz; j++){
- System.out.print(final1[i][j] + "");
- }
- System.out.print("\n");
- }
- System.out.print("\n\n\n\n" + X + ", " + Y + "\n");
- for (int j = 0; j < Y - 1; j++){
- for (int i = 0; i < X - 1; i++){
- System.out.print(pixels[j][i] + "");
- }
- System.out.print("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement