Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int[][] abrirArquivoComprimido() {
- FileDialog fileDialog = new FileDialog(new Frame(), "Abrir arquivo .txt");
- fileDialog.setFile("*.txt");
- fileDialog.setVisible(true);
- if (fileDialog.getFile() != null) {
- String caminho;
- caminho = fileDialog.getDirectory() + fileDialog.getFile();
- try {
- Scanner leitor = new Scanner(new FileReader(caminho));
- // Le a imagem comprimida
- ArrayList<ArrayList<Integer>> imagem = new ArrayList<ArrayList<Integer>>();
- while (leitor.hasNextLine()) {
- String[] numeros = leitor.nextLine().split(" ");
- String saida = "";
- for (int j = 0; j < numeros.length; j++) {
- // lendo os valores dos numeros e suas contagens
- String[] valores = numeros[j].split(",");
- int quantidade = Integer.parseInt(valores[0]);
- int numero = Integer.parseInt(valores[1]);
- for (int k = 0; k < quantidade; k++) {
- saida += numero;
- }
- }
- ArrayList<Integer> linha = new ArrayList<Integer>();
- int j = 0;
- while ((j + 8) < saida.length()) {
- // transformando em decimal
- String binario = saida.substring(j, j + 8);
- int pixel = Integer.parseInt(binario, 2);
- j += 8;
- linha.add(pixel);
- }
- imagem.add(linha);
- }
- altura = imagem.size();
- largura = imagem.get(0).size();
- int[][] matriz = new int[altura][largura];
- for (int i = 0; i < imagem.size(); i++) {
- ArrayList<Integer> linha = imagem.get(i);
- for (int j = 0; j < linha.size(); j++) {
- matriz[i][j] = linha.get(j);
- }
- }
- return matriz;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (NoSuchElementException e) {
- JOptionPane.showMessageDialog(null, "Arquivo incompativel.");
- } catch (IOException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- return null;
- }
Add Comment
Please, Sign In to add comment