Guest User

Untitled

a guest
Jan 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. private int[][] abrirArquivoComprimido() {
  2. FileDialog fileDialog = new FileDialog(new Frame(), "Abrir arquivo .txt");
  3. fileDialog.setFile("*.txt");
  4. fileDialog.setVisible(true);
  5.  
  6. if (fileDialog.getFile() != null) {
  7. String caminho;
  8.  
  9. caminho = fileDialog.getDirectory() + fileDialog.getFile();
  10.  
  11. try {
  12. Scanner leitor = new Scanner(new FileReader(caminho));
  13.  
  14. // Le a imagem comprimida
  15. ArrayList<ArrayList<Integer>> imagem = new ArrayList<ArrayList<Integer>>();
  16. while (leitor.hasNextLine()) {
  17. String[] numeros = leitor.nextLine().split(" ");
  18. String saida = "";
  19. for (int j = 0; j < numeros.length; j++) {
  20. // lendo os valores dos numeros e suas contagens
  21. String[] valores = numeros[j].split(",");
  22. int quantidade = Integer.parseInt(valores[0]);
  23. int numero = Integer.parseInt(valores[1]);
  24. for (int k = 0; k < quantidade; k++) {
  25. saida += numero;
  26. }
  27. }
  28. ArrayList<Integer> linha = new ArrayList<Integer>();
  29. int j = 0;
  30. while ((j + 8) < saida.length()) {
  31. // transformando em decimal
  32. String binario = saida.substring(j, j + 8);
  33. int pixel = Integer.parseInt(binario, 2);
  34. j += 8;
  35. linha.add(pixel);
  36. }
  37. imagem.add(linha);
  38. }
  39.  
  40. altura = imagem.size();
  41. largura = imagem.get(0).size();
  42. int[][] matriz = new int[altura][largura];
  43.  
  44. for (int i = 0; i < imagem.size(); i++) {
  45. ArrayList<Integer> linha = imagem.get(i);
  46. for (int j = 0; j < linha.size(); j++) {
  47. matriz[i][j] = linha.get(j);
  48. }
  49. }
  50.  
  51. return matriz;
  52.  
  53. } catch (FileNotFoundException e) {
  54. e.printStackTrace();
  55. } catch (NoSuchElementException e) {
  56. JOptionPane.showMessageDialog(null, "Arquivo incompativel.");
  57. } catch (IOException e) {
  58. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  59. }
  60. }
  61. return null;
  62. }
Add Comment
Please, Sign In to add comment