Converter bufferedimage em uma matriz de inteiro

3 respostas
tcp

Olá A todos!!!
Sou novo na comunidade! Gostaria de saber qual a melhor forma de converter um bufferimagem em uma matriz de inteiro, tentei usar o getRGB, mais estou tendo problemas com ele.

3 Respostas

W

Se for uma matriz quadrada, você pode fazer assim:

byte [] quatroByte = new byte[4];
		BufferedInputStream bis = null;
		int imgSize = 0;
		int raizQua = 0;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(new File("C://temp//images.jpg")));
		} catch (FileNotFoundException e) {
			System.err.println("Imagem não existe");
			System.exit(0);
		}

		try {
			imgSize = bis.available();
		} catch (IOException e) {
			System.err.println("Não conseguiu ler tamanho da imagem");
			System.exit(0);
		}
		
		// Divide tamanho imagem por 4, acha raiz e arredonda
		raizQua = (int) Math.round(Math.sqrt(imgSize / 4) + 0.5);

		// Faz uma matriz quadrada
		int matrizInt[][] = new int[raizQua][raizQua];
		int lidos = 0;
		
		for (int row = 0; row < raizQua; row++) {

			for (int col = 0; col < raizQua; col++) {

				try {
					
					lidos = bis.read(quatroByte);

					// Converte 4 bytes para inteiro
					matrizInt[row][col] = (quatroByte[0] << 3);
					matrizInt[row][col] |= (quatroByte[1] << 2);
					matrizInt[row][col] |= (quatroByte[2] << 1);
					matrizInt[row][col] |= quatroByte[3];

					// Leu resíduo, fim
					if (lidos < 4) {
						col = raizQua;
						row = raizQua;
					}
					
				} catch (IOException e) {
					System.err.println("Falha no read.");;
				}
				
			}

		}
		
		try {
			bis.close();
		} catch (IOException e) {
			System.err.println("Falha ao fechar imagem");
		}

Fiz este algoritimo e só executei uma vez, você pode avaliar e adaptar a sua necessidade.

Postei artigo sobre manipulação binária em http://oracle2java.blogspot.com.br/2012/10/java-tutorial-para-manipulacao-binaria.html

wiliamps

TerraSkilll

Qual o problema que você está tendo com o getRGB()? Pois ele parece adequado à tarefa, se o que você quer é a matriz de cores correspondente à imagem.

Exemplo:
public int[][] pegarComoArray(){
  int w = image.getWidth(); // largura da imagem
  int h = image.getHeight(); // altura da imagem

  int[][] matriz = new int[w][h];

  for (int i = 0; i < w; i++)
    for (int j = 0; j < h; j++)
        matriz[i][j] = image.getRGB(i, j);

  return matriz;
}
Baseado em [url]http://alvinalexander.com/blog/post/java/getting-rgb-values-for-each-pixel-in-image-using-java-bufferedi[/url].

Abraço.

tcp
TerraSkilll:
Qual o problema que você está tendo com o getRGB()? Pois ele parece adequado à tarefa, se o que você quer é a matriz de cores correspondente à imagem. Exemplo:
public int[][] pegarComoArray(){
  int w = image.getWidth(); // largura da imagem
  int h = image.getHeight(); // altura da imagem

  int[][] matriz = new int[w][h];

  for (int i = 0; i < w; i++)
    for (int j = 0; j < h; j++)
        matriz[i][j] = image.getRGB(i, j);

  return matriz;
}
Baseado em [url]http://alvinalexander.com/blog/post/java/getting-rgb-values-for-each-pixel-in-image-using-java-bufferedi[/url].

Abraço.


Então é que existe outro construtor do .getRGB, este faz a conversão é manda diretamente para a minha matriz ("isto teoricamente") porque ele tem um parâmetro chamado scansize que esta me dando dor de cabeça, pois, ele não aceita 0 nem 1, e ele multiplica a quantidade de campos que eu tenho pelo valor que coloquei no scansize. Mais mesmo assim muito obrigado.

Criado 7 de novembro de 2012
Ultima resposta 8 de nov. de 2012
Respostas 3
Participantes 3