Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.25 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. import java.awt.image.*;
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.awt.Color;
  10. import java.awt.Container;
  11. import java.awt.GridLayout;
  12. import java.awt.image.BufferedImage;
  13. import java.awt.image.RescaleOp;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import javax.imageio.ImageIO;
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JScrollPane;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.awt.BorderLayout;
  24. import java.awt.Dimension;
  25. import java.awt.EventQueue;
  26. import java.awt.Graphics;
  27. import java.awt.Graphics2D;
  28. import java.awt.geom.Rectangle2D;
  29. import java.util.Map;
  30. import java.util.TreeMap;
  31. import java.awt.BasicStroke;
  32. import java.awt.Color;
  33. import java.awt.Dimension;
  34. import java.awt.Graphics;
  35. import java.awt.Graphics2D;
  36. import java.awt.Point;
  37. import java.awt.RenderingHints;
  38. import java.awt.Stroke;
  39. import java.util.ArrayList;
  40. import java.util.List;
  41. import java.util.Random;
  42. import javax.swing.*;
  43.  
  44. /**
  45. *
  46. * @author deboraalmeida
  47. */
  48. class Circle{
  49.  
  50. private int centerX;
  51.  
  52. private int centerY;
  53.  
  54.  
  55.  
  56.  
  57.  
  58. public Circle (int inX, int inY){
  59.  
  60. centerX = inX;
  61.  
  62. centerY = inY;
  63.  
  64.  
  65.  
  66. }
  67.  
  68. public int getCenterX(){
  69.  
  70. return centerX;
  71.  
  72. }
  73.  
  74. public void setCenterX(int centerX){
  75.  
  76. this.centerX = centerX;
  77.  
  78. }
  79.  
  80. public int getCenterY(){
  81.  
  82. return centerY;
  83.  
  84. }
  85.  
  86. public void setCenterY(int centerY){
  87.  
  88. this.centerY = centerY;
  89.  
  90. }
  91.  
  92. @Override
  93.  
  94. public String toString(){
  95.  
  96. return "Circle [centerX=" + centerX + ", centerY=" + centerY + "]";
  97.  
  98. }
  99.  
  100. }
  101. class Exibicao {
  102. //Exibição
  103. // SADO
  104. public static void exibirImagem(BufferedImage imagem) {
  105. ImageIcon icon = new ImageIcon(imagem);
  106. JLabel imagemLabel = new JLabel(icon);
  107. JFrame frame = new JFrame();
  108. Container contentPane = frame.getContentPane();
  109. contentPane.setLayout(new GridLayout());
  110. contentPane.add(new JScrollPane(imagemLabel));
  111. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112. frame.setSize(800, 600);
  113. frame.setVisible(true);
  114. }
  115.  
  116. public static void exibirImagem(BufferedImage imagem, BufferedImage imagem2) {
  117. ImageIcon icon = new ImageIcon(imagem);
  118. JLabel imagemLabel = new JLabel(icon);
  119. ImageIcon icon2 = new ImageIcon(imagem2);
  120. JLabel imagemLabel2 = new JLabel(icon2);
  121. JFrame frame = new JFrame();
  122. Container contentPane = frame.getContentPane();
  123. contentPane.setLayout(new GridLayout());
  124. contentPane.add(new JScrollPane(imagemLabel));
  125. contentPane.add(new JScrollPane(imagemLabel2));
  126. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  127. frame.setSize(1100, 680);
  128. frame.setVisible(true);
  129. }
  130. }
  131. public class PID {
  132. public static BufferedImage EscalaDeCinza(BufferedImage imagem) {
  133. //Imagem resultante
  134. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  135.  
  136. //pegar coluna e linha da imagem
  137. int coluna = imagem.getWidth();
  138. int linha = imagem.getHeight();
  139.  
  140. int media = 0;
  141. //laço para varrer a matriz de pixels da imagem
  142. for (int i = 0; i < coluna; i++) {
  143. for (int j = 0; j < linha; j++) {
  144. //rgb recebe o valor RGB do pixel em questão
  145. int rgb = imagem.getRGB(i, j);
  146. int r = (int)((rgb&0x00FF0000)>>>16); //R
  147. int g = (int)((rgb&0x0000FF00)>>>8); //G
  148. int b = (int) (rgb&0x000000FF); //B
  149.  
  150. //media dos valores do RGB
  151. //será o valor do pixel na nova imagem
  152. media = (r + g + b) / 3;
  153.  
  154. //criar uma instância de Color
  155. Color color = new Color(media, media, media);
  156. //setar o valor do pixel com a nova cor
  157. ResultImage.setRGB(i, j, color.getRGB());
  158. }
  159. }
  160. return ResultImage;
  161. }
  162.  
  163. public static BufferedImage Negativo(BufferedImage imagem) {
  164. //Imagem resultante
  165. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  166.  
  167. //pegar coluna e linha da imagem
  168. int coluna = imagem.getWidth();
  169. int linha = imagem.getHeight();
  170. //laço para varrer a matriz de pixels da imagem
  171. for (int i = 0; i < coluna; i++) {
  172. for (int j = 0; j < linha; j++) {
  173. //rgb recebe o valor RGB do pixel em questão
  174. int rgb = imagem.getRGB(i, j);
  175. //a cor inversa é dado por 255 menos o valor da cor
  176. int r = 255 - (int)((rgb&0x00FF0000)>>>16);
  177. int g = 255 - (int)((rgb&0x0000FF00)>>>8);
  178. int b = 255 - (int) (rgb&0x000000FF);
  179. Color color = new Color(r, g, b);
  180. ResultImage.setRGB(i, j, color.getRGB());
  181. }
  182. }
  183. return ResultImage;
  184. }
  185.  
  186. public static BufferedImage Media (BufferedImage imagem) {
  187. //imagem resultante
  188. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  189.  
  190. //mascara de média
  191. int [][]mascaraMedia = {{1,1,1},
  192. {1,1,1},
  193. {1,1,1}};
  194. //soma dos valores da máscara
  195. int valorMascara = 9;
  196.  
  197. //cores primarias
  198. int r = 0, g = 0, b = 0;
  199.  
  200. //pegar coluna e linha da imagem
  201. int coluna = imagem.getWidth();
  202. int linha = imagem.getHeight();
  203.  
  204. //percorre a imagem
  205. for (int i = 1; i + 1 < linha; i++) {
  206. for (int j = 1; j + 1 < coluna; j++) {
  207. //percorre a máscara
  208. for (int l = -1; l <= 1; l++) {
  209. for (int k = -1; k <= 1; k++) {
  210. //rgb = rgb do pixel
  211. int rgb = imagem.getRGB(j + k, i + l);
  212. //pegando os valores das cores primarias de cada pixel após a convolucao com a máscara
  213. r += (mascaraMedia[1 + l][1 + k] * (int)((rgb&0x00FF0000)>>>16));
  214. g += (mascaraMedia[1 + l][1 + k] * (int)((rgb&0x0000FF00)>>>8));
  215. b += (mascaraMedia[1 + l][1 + k] * (int)((rgb&0x000000FF)));
  216. }
  217. }
  218. //dividia as cores pelo valor da máscara
  219. r = r / valorMascara;
  220. g = g / valorMascara;
  221. b = b / valorMascara;
  222. //nova cor do pixel
  223. Color tempColor = new Color(r, g, b);
  224. //setar o respectivel pixel na nova imagem
  225. ResultImage.setRGB(j, i, tempColor.getRGB());
  226. //zerar valor das cores primarias
  227. r = g = b = 0;
  228. }
  229. }
  230. ResultImage.getSubimage(1, 1, coluna-1, linha-1);
  231. return ResultImage;
  232. }
  233.  
  234. public static BufferedImage Gaussiano (BufferedImage imagem) {
  235. //imagem resultante
  236. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  237. //mascara
  238. int [][]mascaraGaussiano = {{1,2,1},
  239. {2,4,2},
  240. {1,2,1}};
  241. int valorMascara = 16;
  242.  
  243. int r = 0, g = 0, b = 0;
  244. //tamanho imagem
  245. int coluna = imagem.getWidth();
  246. int linha = imagem.getHeight();
  247. //percorre imagem
  248. for (int i = 1; i + 1 < linha; i++) {
  249. for (int j = 1; j + 1 < coluna; j++) {
  250. //percorre mascara
  251. for (int l = -1; l <= 1; l++) {
  252. for (int k = -1; k <= 1; k++) {
  253. //rgb
  254. int rgb = imagem.getRGB(j + k, i + l);
  255. //pegando os valores das cores primarias de cada pixel após a convolucao com a máscara
  256. r += (mascaraGaussiano[1 + l][1 + k] * (int)((rgb&0x00FF0000)>>>16));
  257. g += (mascaraGaussiano[1 + l][1 + k] * (int)((rgb&0x0000FF00)>>>8));
  258. b += (mascaraGaussiano[1 + l][1 + k] * (int)((rgb&0x000000FF)));
  259. }
  260.  
  261. }
  262. //dividindo as cores pelo valor da máscara
  263. r = r / valorMascara;
  264. g = g / valorMascara;
  265. b = b / valorMascara;
  266. Color tempColor = new Color(r, g, b);
  267. //setar novo valor do pixel na imagem resultante
  268. ResultImage.setRGB(j, i, tempColor.getRGB());
  269. r = g = b = 0;
  270. }
  271. }
  272. ResultImage.getSubimage(1, 1, coluna-1, linha-1);
  273. return ResultImage;
  274. }
  275.  
  276. public static BufferedImage Laplaciano (BufferedImage imagem) {
  277. //imagem resultante
  278. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  279. //mascaras
  280. int [][]mascaraL1 = {{0,-1,0},
  281. {-1,4,-1},
  282. {0,-1,0}};
  283.  
  284. int [][]mascaraL2 = {{1,1,1},
  285. {1,-8,1},
  286. {1,1,1}};
  287.  
  288. int r = 0, g = 0, b = 0;
  289. //tamanho imagem
  290. int coluna = imagem.getWidth();
  291. int linha = imagem.getHeight();
  292. //percorre imagem
  293. for (int i = 1; i + 1 < linha; i++) {
  294. for (int j = 1; j + 1 < coluna; j++) {
  295. //percorre mascara
  296. for (int l = -1; l <= 1; l++) {
  297. for (int k = -1; k <= 1; k++) {
  298. //rgb
  299. int rgb = imagem.getRGB(j + k, i + l);
  300. //pegando os valores das cores primarias de cada pixel após a convolucao com a máscara
  301. r += (mascaraL1[1 + l][1 + k] * (int)((rgb&0x00FF0000)>>>16));
  302. g += (mascaraL1[1 + l][1 + k] * (int)((rgb&0x0000FF00)>>>8));
  303. b += (mascaraL1[1 + l][1 + k] * (int)((rgb&0x000000FF)));
  304. }
  305.  
  306. }
  307. //arredondamento de valores
  308. Color tempColor = new Color(Math.min(255, Math.max(0, r)), Math.min(255, Math.max(0, g)), Math.min(255, Math.max(0, b)));
  309. ResultImage.setRGB(j, i, tempColor.getRGB());
  310. r = g = b = 0;
  311. }
  312. }
  313. //percorre imagem
  314. for (int i = 1; i + 1 < imagem.getHeight(); i++) {
  315. for (int j = 1; j + 1 < imagem.getWidth(); j++) {
  316. //percorre mascara
  317. for (int l = -1; l <= 1; l++) {
  318. for (int k = -1; k <= 1; k++) {
  319. //rgb
  320. int rgb = imagem.getRGB(j + k, i + l);
  321. //pegando os valores das cores primarias de cada pixel após a convolucao com a máscara
  322. r += (mascaraL2[1 + l][1 + k] * (int)((rgb&0x00FF0000)>>>16));
  323. g += (mascaraL2[1 + l][1 + k] * (int)((rgb&0x0000FF00)>>>8));
  324. b += (mascaraL2[1 + l][1 + k] * (int)((rgb&0x000000FF)));
  325. }
  326.  
  327. }
  328. //arredondamento de valores
  329. Color tempColor = new Color(Math.min(255, Math.max(0, r)), Math.min(255, Math.max(0, g)), Math.min(255, Math.max(0, b)));
  330. ResultImage.setRGB(j, i, tempColor.getRGB());
  331. r = g = b = 0;
  332. }
  333. }
  334. ResultImage.getSubimage(1, 1, coluna-1, linha-1);
  335. return ResultImage;
  336. }
  337.  
  338. public static BufferedImage Sobel (BufferedImage imagem) {
  339. //imagem resultante
  340. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  341. //mascaras
  342. int [][]mascaraS1 = {{-1,-2,-1},
  343. {0,0,0},
  344. {1,2,1}};
  345. int [][]mascaraS2 = {{-1,0,1},
  346. {-2,0,2},
  347. {-1,0,1}};
  348.  
  349. int r = 0, g = 0, b = 0;
  350. //tamanho da imagem
  351. int coluna = imagem.getWidth();
  352. int linha = imagem.getHeight();
  353.  
  354. //percorre imagem
  355. for (int i = 1; i + 1 < linha; i++) {
  356. for (int j = 1; j + 1 < coluna; j++) {
  357. //percorre mascara
  358. for (int l = -1; l <= 1; l++) {
  359. for (int k = -1; k <= 1; k++) {
  360. //rgb
  361. int rgb = imagem.getRGB(j + k, i + l);
  362. //pegando os valores das cores primarias de cada pixel após a convolucao com a máscara
  363. r += (mascaraS1[1 + l][1 + k] * (int)((rgb&0x00FF0000)>>>16));
  364. g += (mascaraS1[1 + l][1 + k] * (int)((rgb&0x0000FF00)>>>8));
  365. b += (mascaraS1[1 + l][1 + k] * (int)((rgb&0x000000FF)));
  366. }
  367.  
  368. }
  369. //arredondamento de valores
  370. Color tempColor = new Color(Math.min(255, Math.max(0, r)), Math.min(255, Math.max(0, g)), Math.min(255, Math.max(0, b)));
  371. ResultImage.setRGB(j, i, tempColor.getRGB());
  372. r = g = b = 0;
  373. }
  374. }
  375. //percorrer imagem
  376. for (int i = 1; i + 1 < imagem.getHeight(); i++) {
  377. for (int j = 1; j + 1 < imagem.getWidth(); j++) {
  378. //Percorrer máscara
  379. for (int l = -1; l <= 1; l++) {
  380. for (int k = -1; k <= 1; k++) {
  381. //RGB
  382. int rgb = imagem.getRGB(j + k, i + l);
  383. //pegando os valores das cores primarias de cada pixel após a convolucao com a máscara
  384. r += (mascaraS2[1 + l][1 + k] * (int)((rgb&0x00FF0000)>>>16));
  385. g += (mascaraS2[1 + l][1 + k] * (int)((rgb&0x0000FF00)>>>8));
  386. b += (mascaraS2[1 + l][1 + k] * (int)((rgb&0x000000FF)));
  387. }
  388.  
  389. }
  390. //Arredondamento dos valores
  391. Color tempColor = new Color(Math.min(255, Math.max(0, r)), Math.min(255, Math.max(0, g)), Math.min(255, Math.max(0, b)));
  392. ResultImage.setRGB(j, i, tempColor.getRGB());
  393. r = g = b = 0;
  394. }
  395. }
  396. ResultImage.getSubimage(1, 1, coluna-1, linha-1);
  397. return ResultImage;
  398. }
  399.  
  400. public static BufferedImage Convolucao (BufferedImage imagem,int linhas, int colunas, List <Integer> pesos) {
  401. //imagem resultante
  402. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  403. if (linhas != colunas) {
  404. System.out.println("ERRO: A matriz deve ser quadrada e ímpar");
  405. return null;
  406. }else if (linhas % 2 == 0) {
  407. System.out.println("ERRO: A matriz deve ser quadrada e ímpar");
  408. return null;
  409. }else {
  410. //mascara de média
  411. int [][]mascaraConvolucao = new int [linhas][colunas];
  412. int ponteiro = 0;
  413. for (int i = 0; i < linhas; i++){
  414. for (int j = 0; j < colunas; j++) {
  415. mascaraConvolucao[i][j] = pesos.get(ponteiro);
  416. ponteiro++;
  417. }
  418. }
  419. //soma dos valores da máscara
  420. int valorMascara = 0;
  421. for (int i = 0; i < mascaraConvolucao.length; i++) {
  422. for (int j = 0; j < mascaraConvolucao[i].length; j++) {
  423. valorMascara += mascaraConvolucao[i][j];
  424. }
  425. }
  426.  
  427. //cores primarias
  428. int r = 0, g = 0, b = 0;
  429.  
  430. //pegar coluna e linha da imagem
  431. int coluna = imagem.getWidth();
  432. int linha = imagem.getHeight();
  433. int inicial = (int)Math.floor(linhas/2);
  434.  
  435. //percorre a imagem
  436. for (int i = inicial; i + inicial < linha; i++) {
  437. for (int j = inicial; j + inicial < coluna; j++) {
  438. //percorre a máscara
  439. for (int l = -inicial; l <= inicial; l++) {
  440. for (int k = -inicial; k <= inicial; k++) {
  441. //rgb = rgb do pixel
  442. int rgb = imagem.getRGB(j + k, i + l);
  443. //pegando os valores das cores primarias de cada pixel após a convolucao com a máscara
  444. r += (mascaraConvolucao[inicial + l][inicial + k] * (int)((rgb&0x00FF0000)>>>16));
  445. g += (mascaraConvolucao[inicial + l][inicial + k] * (int)((rgb&0x0000FF00)>>>8));
  446. b += (mascaraConvolucao[inicial + l][inicial + k] * (int)((rgb&0x000000FF)));
  447. }
  448. }
  449. //dividia as cores pelo valor da máscara
  450. r = r / valorMascara;
  451. g = g / valorMascara;
  452. b = b / valorMascara;
  453. //nova cor do pixel
  454. Color tempColor = new Color(r, g, b);
  455. //setar o respectivel pixel na nova imagem
  456. ResultImage.setRGB(j, i, tempColor.getRGB());
  457. //zerar valor das cores primarias
  458. r = g = b = 0;
  459. }
  460. }
  461. ResultImage.getSubimage(inicial, inicial, coluna-inicial, linha-inicial);
  462. }
  463. return ResultImage;
  464. }
  465.  
  466. public static BufferedImage Brilho (BufferedImage imagem, float x) {
  467. //imagem resultante
  468. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  469.  
  470. RescaleOp rescaleOp = new RescaleOp(x, 0, null);
  471. rescaleOp.filter(imagem, ResultImage);
  472.  
  473. return ResultImage;
  474. }
  475.  
  476. public static BufferedImage Contraste (BufferedImage imagem, float x) {
  477. //imagem resultante
  478. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  479.  
  480. RescaleOp rescaleOp = new RescaleOp(1.0f, x, null);
  481. rescaleOp.filter(imagem, ResultImage);
  482.  
  483. return ResultImage;
  484. }
  485.  
  486. //----------------------------------------------------------------------
  487. public static BufferedImage LGP (BufferedImage imagem) { //Limiar Global Padrão
  488.  
  489. //imagem resultante
  490. BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  491. int r = 0, g = 0, b = 0, mediar, mediag, mediab, totalpixel;
  492. for (int i = 1; i + 1 < imagem.getHeight(); i++) {
  493. for (int j = 1; j + 1 < imagem.getWidth(); j++) {
  494. //rgb
  495. int rgb = imagem.getRGB(j, i);
  496.  
  497. //percorrer imagem
  498. r += (int)((rgb&0x00FF0000)>>>16);
  499. g += (int)((rgb&0x0000FF00)>>>8);
  500. b += (int)((rgb&0x000000FF));
  501.  
  502. }
  503. }
  504. totalpixel = imagem.getHeight() * imagem.getWidth();
  505. mediar = Math.round(r/ totalpixel);
  506. mediag = Math.round(g/ totalpixel);
  507. mediab = Math.round(b/ totalpixel);
  508. for (int i = 1; i + 1 < imagem.getHeight(); i++) {
  509. for (int j = 1; j + 1 < imagem.getWidth(); j++) {
  510. //rgb
  511. int rgb = imagem.getRGB(j, i);
  512.  
  513. //percorrer imagem
  514. r = (int)((rgb&0x00FF0000)>>>16);
  515. if(r < mediar){
  516. r = 0;
  517. }else if(r > mediar){
  518. r = 255;
  519. }
  520. g = (int)((rgb&0x0000FF00)>>>8);
  521. if(g < mediag){
  522. g = 0;
  523. }else if(g > mediag){
  524. g = 255;
  525. }
  526. b = (int)((rgb&0x000000FF));
  527. if(b < mediab){
  528. b = 0;
  529. }else if(b >= mediab){
  530. b = 255;
  531. }
  532. //nova cor do pixel
  533. Color tempColor = new Color(r, g, b);
  534. //setar o respectivel pixel na nova imagem
  535. ResultImage.setRGB(j, i, tempColor.getRGB());
  536.  
  537. }
  538. }
  539. return ResultImage;
  540. }
  541.  
  542. //----------------------------------------------------------------------
  543. public static int maxVal(int[] a){
  544. int b = 0;
  545. for(int i = 1; i < a.length; i++){
  546. if(a[i] > b){
  547. b = a[i];
  548. }
  549. }
  550. return b;
  551. }
  552. //----------------------------------------------------------------------
  553. public static BufferedImage Histograma (BufferedImage imagem) {
  554.  
  555. //imagem resultante
  556. BufferedImage ResultImage = new BufferedImage (imagem.getWidth(),imagem.getHeight(),BufferedImage.TYPE_INT_RGB);
  557. //BufferedImage ResultImage = new BufferedImage (imagem.getColorModel(),imagem.copyData(null),imagem.getColorModel().isAlphaPremultiplied(),null);
  558.  
  559. int width = imagem.getWidth();
  560. int height = imagem.getHeight();
  561. int padding = 40;
  562. int labelPadding = 40;
  563. Color lineColor = new Color(44, 102, 230, 180);
  564. Color pointColor = new Color(100, 100, 100, 180);
  565. Color gridColor = new Color(200, 200, 200, 200);
  566. final Stroke GRAPH_STROKE = new BasicStroke(4f);
  567. int pointWidth = 8;
  568. int numberYDivisions = 10;
  569. int[] scores = new int[6];
  570. for(int i = 0; i < scores.length; i++){
  571. scores[i] = 0;
  572. }
  573. int r = 0, g =0, b = 0;
  574. //Fazer os scores
  575. for (int i = 1; i + 1 < imagem.getHeight(); i++) {
  576. for (int j = 1; j + 1 < imagem.getWidth(); j++) {
  577. //rgb
  578. int rgb = (int)imagem.getRGB(j, i);
  579. r = (int)((rgb&0x00FF0000)>>>16);
  580. g = (int)((rgb&0x0000FF00)>>>8);
  581. b = (int)((rgb&0x000000FF));
  582.  
  583. if (r <= 50 ||
  584. g <= 50 ||
  585. b <= 50){
  586. scores[0]++;
  587. }else if( (r > 50 && r <= 100) ||
  588. (g > 50 && g <= 100) ||
  589. (b > 50 && b <= 100) ){
  590. scores[2]++;
  591. }else if((r > 100 && r <= 150) ||
  592. (g > 100 && g <= 150) ||
  593. (b > 100 && b <= 150) ){
  594. scores[3]++;
  595. }else if((r > 150 && r <= 200) ||
  596. (g > 150 && g <= 200) ||
  597. (b > 150 && b <= 200) ){
  598. scores[4]++;
  599. }else if ((r > 200 ||
  600. g > 200 ||
  601. b > 200)){
  602. scores[5]++;
  603. }
  604.  
  605. }
  606. }
  607. for(int i =0; i < scores.length; i++){
  608. System.out.println(scores[i]);
  609. }
  610. Graphics2D g2 = ResultImage.createGraphics(); // <--
  611. g2.setColor(Color.WHITE);
  612. g2.fillRect ( 0, 0, width, height );
  613. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  614.  
  615. double xScale = ((double) width - (2 * padding) - labelPadding) / (scores.length - 1);
  616. double yScale = ((double) height - 2 * padding - labelPadding) / (maxVal(scores) - 0);
  617.  
  618. List<Point> graphPoints = new ArrayList<>();
  619. for (int i = 0; i < scores.length; i++) {
  620. int x1 = (int) (i * xScale + padding + labelPadding);
  621. int y1 = (int) ((maxVal(scores) - scores[i]) * yScale + padding);
  622. graphPoints.add(new Point(x1, y1));
  623. }
  624.  
  625. g2.setColor(Color.WHITE);
  626. g2.fillRect(padding + labelPadding, padding, width - (2 * padding) - labelPadding, height - 2 * padding - labelPadding);
  627. g2.setColor(Color.BLACK);
  628.  
  629. // create hatch marks and grid lines for y axis.
  630. for (int i = 0; i < numberYDivisions + 1; i++) {
  631. int x0 = padding + labelPadding;
  632. int x1 = pointWidth + padding + labelPadding;
  633. int y0 = height - ((i * (height - padding * 2 - labelPadding)) / numberYDivisions + padding + labelPadding);
  634. int y1 = y0;
  635. if (scores.length > 0) {
  636. g2.setColor(gridColor);
  637. g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, width - padding, y1);
  638. g2.setColor(Color.BLACK);
  639. // String yLabel = ((int) ((0 + (maxVal(scores) - 0) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + "";
  640. String yLabel = ((int) (maxVal(scores) / numberYDivisions)) * i + "";
  641. FontMetrics metrics = g2.getFontMetrics();
  642. int labelWidth = metrics.stringWidth(yLabel);
  643. g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3);
  644.  
  645. }
  646.  
  647.  
  648.  
  649. g2.drawLine(x0, y0, x1, y1);
  650. }
  651.  
  652. // and for x axis
  653. int []labels = {0, 50, 100, 150, 200, 255};
  654. for (int i = 0; i < scores.length; i++) {
  655. if (scores.length > 1) {
  656. int x0 = i * (width - padding * 2 - labelPadding) / (scores.length - 1) + padding + labelPadding;
  657. int x1 = x0;
  658. int y0 = height - padding - labelPadding;
  659. int y1 = y0 - pointWidth;
  660. if ((i % ((int) ((scores.length / 20.0)) + 1)) == 0) {
  661. g2.setColor(gridColor);
  662. g2.drawLine(x0, height - padding - labelPadding - 1 - pointWidth, x1, padding);
  663. g2.setColor(Color.BLACK);
  664. String xLabel = labels[i] + "";
  665. FontMetrics metrics = g2.getFontMetrics();
  666. int labelWidth = metrics.stringWidth(xLabel);
  667. g2.drawString(xLabel, x0 - labelWidth / 2, y0 + metrics.getHeight() + 3);
  668.  
  669.  
  670. }
  671. g2.drawLine(x0, y0, x1, y1);
  672. }
  673. }
  674.  
  675. // create x and y axes
  676. g2.drawLine(padding + labelPadding, height - padding - labelPadding, padding + labelPadding, padding);
  677. g2.drawLine(padding + labelPadding, height - padding - labelPadding, width - padding, height - padding - labelPadding);
  678.  
  679. Stroke oldStroke = g2.getStroke();
  680. g2.setColor(lineColor);
  681. g2.setStroke(GRAPH_STROKE);
  682. for (int i = 0; i < graphPoints.size(); i++) {
  683. int x1 = graphPoints.get(i).x;
  684. int y1 = graphPoints.get(i).y;
  685. int x2 = i * (width - padding * 2 - labelPadding) / (scores.length - 1) + padding + labelPadding;
  686. int y2 = height - padding - labelPadding;
  687. g2.drawLine(x1, y1, x2, y2);
  688. }
  689.  
  690. g2.setStroke(oldStroke);
  691. g2.setColor(pointColor);
  692. for (int i = 0; i < graphPoints.size(); i++) {
  693. int x = graphPoints.get(i).x - pointWidth / 2;
  694. int y = graphPoints.get(i).y - pointWidth / 2;
  695. int ovalW = pointWidth;
  696. int ovalH = pointWidth;
  697. g2.fillOval(x, y, ovalW, ovalH);
  698. }
  699.  
  700.  
  701.  
  702. return ResultImage;
  703. }
  704.  
  705. //----------------------------------------------------------------------
  706.  
  707. public int[][] findEdges(int[][] sourceArray){
  708.  
  709. double deltaSquaredThreshold = 800; // if dx^2 + dy^2 > threshold, call it an edge.
  710.  
  711.  
  712.  
  713. int[][] edgeArray = ImageManager.createGrayscaleArrayOfSize(sourceArray.length-1,
  714.  
  715. sourceArray[0].length-1);
  716.  
  717. // TODO: insert your code here.
  718.  
  719.  
  720.  
  721. for (int i = 1; i < sourceArray.length - 1; i++){
  722.  
  723. for (int j = 1; j < sourceArray[0].length - 1; j++){
  724.  
  725. int Dx = sourceArray[i][j] - sourceArray[i - 1][j];
  726.  
  727. int Dy = sourceArray[i][j] - sourceArray[i][j - 1];
  728.  
  729. int magnitude = (int) (Math.pow(Dx, 2) + Math.pow(Dy, 2));
  730.  
  731. if (magnitude > 800){
  732.  
  733. edgeArray[i][j] = 255;
  734.  
  735. }else{
  736.  
  737. edgeArray[i][j] = 0;
  738.  
  739. }
  740.  
  741. }
  742.  
  743. }
  744.  
  745.  
  746.  
  747.  
  748.  
  749. //-------------------------------------------------
  750.  
  751. return edgeArray;
  752.  
  753. }
  754.  
  755. public ArrayList<Circle> findBestCircles(int [][] houghArray){
  756.  
  757. int maxNumCirlces = 10; //the most circles you're hoping to get (adjust this)
  758.  
  759. int votesThreshold = 60; // the minimum number of votes required to count as a "found" circle.
  760.  
  761. int annihilationRadius = 15; // after you find a maximum in the hough array (and presumably do something with it),
  762.  
  763. // wipe out all the votes within this radius of the vote winner to zero, so that
  764.  
  765. // you are ready to get the next maximum. (Finding two maxima within a couple of pixels
  766.  
  767. // is unlikely to be useful and more likely to be natural/rounding error.)
  768.  
  769. int[][] houghCopy = ImageManager.deepCopyArray(houghArray);
  770.  
  771. ArrayList<Circle> listOfCircles = new ArrayList<Circle>();
  772.  
  773. int max = 0;
  774.  
  775. int maxX = 0;
  776.  
  777. int maxY = 0;
  778.  
  779.  
  780.  
  781. while (listOfCircles.size() < maxNumCirlces){
  782.  
  783. max = 0;
  784.  
  785. maxX = 0;
  786.  
  787. maxY = 0;
  788.  
  789. for (int i = 1; i < houghArray.length; i++){
  790.  
  791. for (int j = 1; j < houghArray[0].length; j++){
  792.  
  793. if (houghArray[i][j] > max){
  794.  
  795. max = houghArray[i][j];
  796.  
  797. maxX = j;
  798.  
  799. maxY = i;
  800.  
  801. }
  802.  
  803. }
  804.  
  805. }
  806.  
  807. if (max > votesThreshold){
  808.  
  809. listOfCircles.add(new Circle(maxX,maxY));
  810.  
  811. }else{
  812.  
  813. break;
  814. }
  815.  
  816. for (int j = maxX -15; j < maxX + 15; j++){
  817.  
  818. for (int i = maxY - 15; i < maxY + 15; i++){
  819.  
  820. int Dx = maxX - j;
  821.  
  822. int Dy = maxY - i;
  823.  
  824. int magnitude = (int)(Math.pow(Dx, 2) + Math.pow(Dy, 2));
  825.  
  826. if(0 < annihilationRadius){
  827.  
  828. houghArray[i][j] = 0;
  829.  
  830. }
  831.  
  832. }
  833.  
  834. }
  835.  
  836. }
  837.  
  838. //---------------------------
  839.  
  840. return listOfCircles;
  841. }
  842.  
  843. public int[][][] buildResult(int [][][] RGBSource, ArrayList<Circle> circleList){
  844.  
  845. int[][][] result = ImageManager.deepCopyArray(RGBSource);
  846.  
  847. // for each location in the circle list, set the corresponding pixel in result to be red (255,0,0).
  848.  
  849. //------------
  850.  
  851. return result;
  852.  
  853. }
  854.  
  855. public int[][] normalizeArrayTo255(int[][] unnormalized){
  856.  
  857. int max =0;
  858.  
  859. for (int i = 1; i < unnormalized.length; i++){
  860.  
  861. for (int j = 1; j < unnormalized[0].length; j++){
  862.  
  863. if (unnormalized[i][j] > max){
  864.  
  865. max = unnormalized[i][j];
  866.  
  867. }
  868.  
  869. }
  870.  
  871. }
  872.  
  873. //-----------------------------------------
  874.  
  875. if (max == 0){
  876.  
  877. throw new RuntimeException("Could not normalize the array to 0 to 255; array was empty.");
  878. }
  879. int [][] normalized = new int[unnormalized.length][unnormalized[0].length];
  880.  
  881. for (int i = 1; i < unnormalized.length; i++){
  882.  
  883. for (int j = 1; j < unnormalized[0].length; j++){
  884.  
  885. normalized[i][j] = (unnormalized[i][j]*255)/max;
  886.  
  887. }
  888.  
  889. }
  890.  
  891. //
  892.  
  893. return normalized;
  894.  
  895. }
  896.  
  897. public BufferedImage generateHough (BufferedImage imagem){
  898.  
  899. BufferedImage sourceImg = new BufferedImage(imagem.getWidth(),imagem.getHeight(), BufferedImage.TYPE_INT_RGB), edgeImg, houghImg = new BufferedImage (imagem.getWidth(),imagem.getHeight(),BufferedImage.TYPE_INT_RGB), resultImg;
  900. ArrayList<Circle> foundCircles;
  901.  
  902. int r = 0, g = 0, b = 0, rgb;
  903. for (int i = 1; i + 1 < imagem.getHeight(); i++) {
  904. for (int j = 1; j + 1 < imagem.getWidth(); j++) {
  905. //rgb
  906. rgb = imagem.getRGB(j, i);
  907. //percorrer imagem
  908. r = (int)((rgb&0x00FF0000)>>>16);
  909. g = (int)((rgb&0x0000FF00)>>>8);
  910. b = (int)((rgb&0x000000FF));
  911. //nova cor do pixel
  912. Color tempColor = new Color(r, g, b);
  913. //setar o respectivel pixel na nova imagem
  914. sourceImg.setRGB(j, i, tempColor.getRGB());
  915. }
  916. }
  917.  
  918. edgeImg = EscalaDeCinza(sourceImg);
  919.  
  920. int TARGET_RADIUS = 27;
  921.  
  922.  
  923. for (int i = 1; i < edgeImg.getHeight(); i++){
  924.  
  925. for (int j = 1; j < edgeImg.getWidth(); j++){
  926. //rgb
  927. rgb = edgeImg.getRGB(j, i);
  928. //percorrer imagem
  929. r = (int)((rgb&0x00FF0000)>>>16);
  930. g = (int)((rgb&0x0000FF00)>>>8);
  931. b = (int)((rgb&0x000000FF));
  932.  
  933. if (r == 255 || g == 255 || b == 255){
  934.  
  935. for (int k = i - 27; k < i + 28; k++){
  936.  
  937. for (int l = j - 27; l < j + 28; l++){
  938.  
  939. if (k >= 0 && k < edgeImg.getHeight()){
  940.  
  941. if (l >= 0 && l < edgeImg.getWidth()){
  942.  
  943. int X = i - k;
  944.  
  945. int Y = j - l;
  946.  
  947. int magnitude = (int)(Math.pow(X, 2) + Math.pow(Y, 2));
  948.  
  949. if ((int)Math.sqrt(magnitude) == 27){
  950.  
  951. //nova cor do pixel
  952. Color tempColor = new Color(r-1, g-1, b-1);
  953. //setar o respectivel pixel na nova imagem
  954. houghImg.setRGB(k, l, tempColor.getRGB());
  955.  
  956.  
  957. }
  958.  
  959. }
  960. }
  961.  
  962. }
  963.  
  964. }
  965.  
  966. }
  967.  
  968. }
  969.  
  970. }
  971.  
  972. // ----------------------------
  973.  
  974. return houghImg;
  975.  
  976. }
  977.  
  978.  
  979. //----------------------------------------------------------------------
  980. public static void main(String[] args) throws IOException{
  981. try {
  982. //carrega nova imagem
  983. BufferedImage imagem = ImageIO.read(new File("insta.png"));
  984. //instancia um filtro e aplica a escala de cinza
  985. PID filtro = new PID();
  986. BufferedImage nova = filtro.generateHough(imagem);
  987.  
  988. if (nova != null) {
  989. ImageIO.write(nova,"png",new File("imagem2.jpg"));
  990. //aqui apenas para demonstração,
  991. //carreguei novamente as duas imagemns para exibi-las dentro de um JFrame
  992. imagem = ImageIO.read(new File("insta.png"));
  993. BufferedImage imagem2 = ImageIO.read(new File("imagem2.jpg"));
  994. Exibicao show = new Exibicao();
  995. show.exibirImagem(imagem, imagem2);
  996. System.out.println("Filtro aplicado com sucesso!");
  997. }
  998. }catch(IOException e){
  999. System.out.println("Erro! Verifique se o arquivo especificado existe e tente novamente.");
  1000. }
  1001. catch(Exception e){
  1002. System.out.println("Erro! " + e.getMessage());
  1003. e.printStackTrace();
  1004. }
  1005. }
  1006.  
  1007. }
  1008. class ImageManager{
  1009.  
  1010. public static final int RED = 0;
  1011.  
  1012. public static final int GREEN = 1;
  1013.  
  1014. public static final int BLUE = 2;
  1015.  
  1016. // ================================== CREATE ARRAYS ===============================
  1017.  
  1018.  
  1019.  
  1020. public static int[][][] createRGBArrayOfSize(int rows, int columns){
  1021.  
  1022. return new int[rows][columns][3];
  1023.  
  1024. }
  1025.  
  1026.  
  1027.  
  1028. public static int [][] createGrayscaleArrayOfSize(int rows, int columns){
  1029.  
  1030. return new int[rows][columns];
  1031.  
  1032. }
  1033.  
  1034.  
  1035.  
  1036. public static BufferedImage createBufferedImageOfSize(int width, int height){
  1037.  
  1038. return new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
  1039.  
  1040. }
  1041.  
  1042. // ================================== DEEP COPY ARRAYS ==============================
  1043.  
  1044.  
  1045.  
  1046. public static int[][][] deepCopyArray(int[][][] source){
  1047.  
  1048. int[][][] result = new int[source.length][source[0].length][source[0][0].length];
  1049.  
  1050. for (int r=0; r < source.length; r++){
  1051.  
  1052. for (int c=0; c<source[0].length; c++){
  1053.  
  1054. for (int z=0; z<source[0][0].length; z++){
  1055.  
  1056. result[r][c][z] = source[r][c][z];
  1057. }
  1058. }
  1059. }
  1060. return result;
  1061.  
  1062. }
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068. public static int[][] deepCopyArray(int[][] source){
  1069.  
  1070. int[][] result = new int[source.length][source[0].length];
  1071.  
  1072. for (int r=0; r < source.length; r++){
  1073.  
  1074. for (int c=0; c<source[0].length; c++){
  1075.  
  1076. result[r][c] = source[r][c];
  1077. }
  1078. }
  1079. return result;
  1080.  
  1081. }
  1082.  
  1083.  
  1084.  
  1085. static BufferedImage deepCopy(BufferedImage bi) {
  1086.  
  1087. ColorModel cm = bi.getColorModel();
  1088.  
  1089. boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
  1090.  
  1091. WritableRaster raster = bi.copyData(null);
  1092.  
  1093. return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
  1094.  
  1095. }
  1096.  
  1097. // ================================== COLOR DEPTH CONVERSIONS =====================
  1098.  
  1099.  
  1100.  
  1101. public static int[][] toGrayArray(int[][][] colorArray){
  1102.  
  1103. int[][] grays = new int[colorArray.length][colorArray[0].length];
  1104.  
  1105. for (int r = 0; r<colorArray.length; r++){
  1106.  
  1107. for (int c=0; c<colorArray[0].length; c++){
  1108.  
  1109. grays[r][c] = (colorArray[r][c][0]+colorArray[r][c][1]+colorArray[r][c][2])/3;
  1110. }
  1111. }
  1112.  
  1113. return grays;
  1114.  
  1115. }
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121. public static int[][][] toColorArray(int[][] grayArray){
  1122.  
  1123. int [][][] colors = new int[grayArray.length][grayArray[0].length][3];
  1124.  
  1125. for (int r = 0; r<grayArray.length; r++){
  1126.  
  1127. for (int c=0; c<grayArray[0].length; c++){
  1128.  
  1129. for (int z=0; z<3; z++){
  1130.  
  1131. colors[r][c][z] = grayArray[r][c];
  1132. }
  1133. }
  1134. }
  1135.  
  1136. return colors;
  1137.  
  1138. }
  1139.  
  1140.  
  1141.  
  1142. // ================================== LOAD IMAGES AND ARRAYS ======================
  1143.  
  1144.  
  1145.  
  1146. public static int[][][] RGBArrayFromFile(String filename){
  1147.  
  1148. return RGBArrayFromImage(loadImage(filename));
  1149.  
  1150. }
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156. public static int[][][] RGBArrayFromFile(File file){
  1157.  
  1158. return RGBArrayFromImage(loadImage(file));
  1159.  
  1160. }
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166. public static int[][] grayscaleArrayFromFile(String filename){
  1167.  
  1168. return grayscaleArrayFromImage(loadImage(filename));
  1169. }
  1170.  
  1171.  
  1172.  
  1173. public static int[][] grayscaleArrayFromFile(File file){
  1174.  
  1175. return grayscaleArrayFromImage(loadImage(file));
  1176.  
  1177. }
  1178.  
  1179.  
  1180.  
  1181. public static BufferedImage loadImage(String filename){
  1182.  
  1183. File theFile = new File(filename);
  1184.  
  1185. return loadImage(theFile);
  1186.  
  1187. }
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193. public static BufferedImage loadImage(File file){
  1194.  
  1195. BufferedImage sourceImage = null;
  1196.  
  1197. try{
  1198.  
  1199.  
  1200.  
  1201.  
  1202.  
  1203. if (file.canRead()){
  1204.  
  1205. sourceImage = ImageIO.read(file);
  1206.  
  1207. }else{
  1208.  
  1209. throw new RuntimeException("Could not open file.");
  1210. }
  1211. }catch (IOException e){
  1212.  
  1213. e.printStackTrace();
  1214.  
  1215. }
  1216.  
  1217.  
  1218.  
  1219. return sourceImage;
  1220. }
  1221.  
  1222. // ==================================== IMAGE TO ARRAY ================================
  1223.  
  1224.  
  1225.  
  1226. public static int[][][] RGBArrayFromImage(BufferedImage source){
  1227.  
  1228. int [][][] rgbArray = new int [source.getHeight()][source.getWidth()][3];
  1229.  
  1230. System.out.println(source.getHeight()+","+source.getWidth());
  1231.  
  1232. for (int r=0; r<source.getHeight(); r++){
  1233.  
  1234. for (int c=0; c<source.getWidth(); c++){
  1235.  
  1236. rgbArray[r][c][0] = (source.getRGB(c, r) >> 16)& 255;
  1237.  
  1238. rgbArray[r][c][1] = (source.getRGB(c, r) >> 8)& 255;
  1239.  
  1240. rgbArray[r][c][2] = (source.getRGB(c, r) >> 0)& 255;
  1241.  
  1242. }
  1243. }
  1244. return rgbArray;
  1245.  
  1246. }
  1247.  
  1248.  
  1249.  
  1250. public static int[][] grayscaleArrayFromImage(BufferedImage source){
  1251.  
  1252. int [][] grayArray = new int [source.getHeight()][source.getWidth()];
  1253.  
  1254. for (int r=0; r<source.getHeight(); r++){
  1255.  
  1256. for (int c=0; c<source.getWidth(); c++){
  1257.  
  1258.  
  1259. grayArray[r][c] = (((source.getRGB(c, r) )& 255) + ((source.getRGB(c, r) >> 8)& 255) + ((source.getRGB(c, r) >> 16)& 255))/3;
  1260. }
  1261. }
  1262.  
  1263. return grayArray;
  1264.  
  1265. }
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271. // ===================================== ARRAY TO IMAGE =============================
  1272.  
  1273.  
  1274.  
  1275. public static BufferedImage ImageFromArray(int [][][] inArray){ // RGB version
  1276.  
  1277. int width = inArray[0].length;
  1278.  
  1279. int height = inArray.length;
  1280.  
  1281. BufferedImage destination = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
  1282.  
  1283. for (int r=0; r<height; r++){
  1284.  
  1285. for (int c=0; c<width; c++){
  1286.  
  1287. destination.setRGB(c, r, (inArray[r][c][2])+(inArray[r][c][1]<<8)+(inArray[r][c][0]<<16));
  1288.  
  1289. }
  1290. }
  1291. return destination;
  1292.  
  1293. }
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299. public static BufferedImage ImageFromArray(int [][] inArray){ //grayscale version
  1300.  
  1301. int width = inArray[0].length;
  1302.  
  1303. int height = inArray.length;
  1304.  
  1305. BufferedImage destination = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
  1306.  
  1307. for (int r=0; r<height; r++){
  1308.  
  1309. for (int c=0; c<width; c++){
  1310.  
  1311. destination.setRGB(c, r, (inArray[r][c])+(inArray[r][c]<<8)+(inArray[r][c]<<16));
  1312. }
  1313. }
  1314.  
  1315. return destination;
  1316.  
  1317. }
  1318.  
  1319.  
  1320. // ==================================== SAVE ===================================
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326. public static void saveImage(int[][][] inArray, String filename) throws IOException{
  1327.  
  1328. saveImage(ImageFromArray(inArray),filename);
  1329.  
  1330. }
  1331.  
  1332.  
  1333.  
  1334. public static void saveImage(int[][] inArray, String filename) throws IOException{
  1335.  
  1336. saveImage(ImageFromArray(inArray),filename);
  1337.  
  1338. }
  1339.  
  1340.  
  1341.  
  1342. public static void saveImage(BufferedImage image, String filename) throws IOException{
  1343.  
  1344. System.out.println(filename);
  1345.  
  1346. int prev = -1;
  1347.  
  1348. while (filename.indexOf(".",prev+1)>-1){
  1349.  
  1350. System.out.println(prev);
  1351.  
  1352. prev = filename.indexOf(".", prev+1);
  1353.  
  1354. }
  1355.  
  1356.  
  1357.  
  1358. if (prev == -1){
  1359.  
  1360. throw new RuntimeException("Attempted to save a file with out a suffix.");
  1361. }
  1362.  
  1363. String suffix = filename.substring(prev+1).toLowerCase();
  1364.  
  1365. if (!suffix.equals("png") && !suffix.equals("jpg") && !suffix.equals("gif") && !suffix.equals("jpeg")){
  1366.  
  1367. throw new RuntimeException("Invalid suffix: \""+suffix+"\"");
  1368. }
  1369.  
  1370.  
  1371. File outputfile = new File(filename);
  1372.  
  1373. ImageIO.write(image, suffix, outputfile);
  1374.  
  1375. }
  1376.  
  1377.  
  1378.  
  1379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement