Problema ao somar 2 imagens - RESOLVIDO!

3 respostas
Y

Boa tarde, eu preciso fazer o seguinte programa:

Programa para soma de duas imagens (tons de cinza) com resoluções possiveis de 1024x1024, 2048x2048 ou 4096x4096.

A ideia é a simples. Tenho 2 imagens de tamanhos iguais (pra facilitar), e trato cada imagem como uma matriz de pixel. Pego um pixel em um ponto da primeira imagem e somo com o pixel desse mesmo ponto da segunda imagem e jogo numa terceira imagem.

Já pesquisei em diversos sites, foruns mas até agora não consegui terminar de fato o programa. Está quase pronto, mas não estou conseguindo fazer mostrar na tela o buffered image da imagem final bfi3 (a matriz com o resultado da soma). Só aparece uma imagem branca. :(

Caso alguém enxergar meu erro e me ajudar seria de grande ajuda.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.io.File;
import javax.imageio.ImageIO;




public class Main {

   
    public static void main(String[] args) {
        // TODO code application logic here
         Image img01 = null;
         Image img02 = null;

  try {
    
      img01 = ImageIO.read(new File("imagem.jpg"));
      img02 = ImageIO.read(new File("imagem2.jpg"));
  }
  catch (Exception e) {
     // TODO: handle exception
  }

  int pixel1;
  int pixel2;
  int widthImg01 = img01.getWidth(null);
  int widthImg02 = img02.getWidth(null);
  int heightImg01 = img01.getHeight(null);
  int heightImg02 = img02.getWidth(null);

  int img03[][] = new int [widthImg01][heightImg01] ;
  int totalwidth = widthImg01 + widthImg02;
  int totalHeigh = heightImg01;

  Desenha desenha = new Desenha();
 
 //Acusa erro caso as imagens sejam de tamanhos diferentes
  if (widthImg01 != widthImg02)
      System.out.println("Imagens de tamanho diferentes!");

  BufferedImage bfi = new BufferedImage(widthImg01, heightImg01, BufferedImage.TYPE_INT_ARGB);
  BufferedImage bfi2 = new BufferedImage(widthImg02, heightImg02, BufferedImage.TYPE_INT_ARGB);
  BufferedImage bfi3 = new BufferedImage(widthImg01, heightImg01, BufferedImage.TYPE_INT_ARGB);

  int tamanho = 0;
  for (int i=0; i<widthImg01; i++){
      for (int j=0; j<heightImg01; j++){

             pixel1 = bfi.getRGB(i,j);
             pixel2 = bfi2.getRGB(i, j);
             
             img03[i][j] = pixel1 + pixel2;
                 
      }
  }

   bfi3 = desenha.setPixelEscalaDeCinza(img03);

               //Ajustes de Imagem
  Graphics g = bfi3.getGraphics();
    
  ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
  ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

  // Preenche o plano de fundo da pagina com branco
  //g.setColor(Color.WHITE);
  //g.fillRect(0, 0, totalwidth,totalHeigh);
  g.fillRect(0,0, widthImg01, heightImg01);

  g.drawImage(bfi3, 0, 0, null);
 
  //g.drawImage(img02, widthImg01, 0, null);


  //Exibe o resultado final
  JFrame fr = new JFrame("Merge de imagens");
  fr.getContentPane().setLayout(new BorderLayout());
  JLabel jl = new JLabel(new ImageIcon(bfi3));
  fr.getContentPane().add(jl,BorderLayout.CENTER);
  fr.pack();
  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  fr.setVisible(true);
  }

}

A classe Desenha

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class Desenha {
    public  BufferedImage setPixelEscalaDeCinza(int[][] img03) {
     int largura = img03.length;
       int altura = img03[0].length;

      BufferedImage image = new BufferedImage(largura, altura,  BufferedImage.TYPE_BYTE_GRAY);

       WritableRaster raster = image.getRaster();
       for (int h = 0; h < largura; h++) {
           for (int w = 0; w < altura; w++) {
               raster.setSample(h, w, 0, img03[h][w]);
           }
       }

      return image;
 }

}

PS: O código está bagunçado pq já mudei ele mtooo, mas isso corrijo dois!

Abraço e vlw
:)

3 Respostas

marcelo.bellissimo

Você está criando uma nova imagem, percebeu? Ele cria uma imagem “zerada” com todos os bytes iguais á 0… vai sair uma imagem toda preta…

BufferedImage bfi = new BufferedImage(widthImg01, heightImg01, BufferedImage.TYPE_INT_ARGB); BufferedImage bfi2 = new BufferedImage(widthImg02, heightImg02, BufferedImage.TYPE_INT_ARGB);

O quadrado branco que você está vendo deve ser culpa disso aqui:

g.fillRect(0,0, widthImg01, heightImg01);
Marky.Vasconcelos

Voce precisa pegar a instancia de uma imagem, com 2 imagens vazias seu resultado ainda sera uma imagem vazia.

BufferedImage bfi = ImageIO.read(new File("C:/img1.png"));   
  BufferedImage bfi2 =ImageIO.read(new File("C:/img2.png"));
Y

Obrigado pela atenção dos que me responderam. Me ajudaram em algumas coisas.
Consegui terminar o código dpois de um relativo esforço e uma ajuda razoável do meu irmão… hahaha
To disponibilizando o código caso alguém precise.
:wink:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.io.File;
import javax.imageio.ImageIO;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Image img01 = null;
        Image img02 = null;

        BufferedImage bfi = null;
//        BufferedImage bfi1 = null;
//        BufferedImage bfi2 = null;
        BufferedImage bfi3 = null;

        try {
            img01 = ImageIO.read(new File("img01.bmp"));
            img02 = ImageIO.read(new File("img02.bmp"));
        } catch (Exception e) {
            // TODO: handle exception
        }

        int pixel1;
        int pixel2;
        int widthImg01 = img01.getWidth(null);
        int widthImg02 = img02.getWidth(null);
        int heightImg01 = img01.getHeight(null);
        int heightImg02 = img02.getHeight(null);


        int totalwidth = widthImg01 * 3;// widthImg02;
        int totalHeigh = heightImg01;

        //Acusa erro caso as imagens sejam de tamanhos diferentes
        if ((widthImg01 != widthImg02) || (heightImg01 != heightImg02)) {
            System.out.println("Imagens de tamanho diferentes!");
        }

        bfi = new BufferedImage(totalwidth, totalHeigh, BufferedImage.TYPE_INT_ARGB);
//        BufferedImage bfi1 = createBufferedImage(img01);
//        BufferedImage bfi2 = createBufferedImage(img02);
        BufferedImage bfi1 = toBufferedImage(img01);
        BufferedImage bfi2 = toBufferedImage(img02);

        bfi3 = new BufferedImage(widthImg01, heightImg01, BufferedImage.TYPE_INT_ARGB);

        // handlepixels(img01, 0, 0, widthImg01, heightImg01);


        for (int j = 0; j < heightImg01; j++) {
            for (int i = 0; i < widthImg01; i++) {

                pixel1 = bfi1.getRGB(i, j);
                pixel2 = bfi2.getRGB(i, j);
                Color color1 = new Color(pixel1);
                Color color2 = new Color(pixel2);

                int red   = (color1.getRed()   + color2.getRed())   % 256;
                int green = (color1.getGreen() + color2.getGreen()) % 256;
                int blue  = (color1.getBlue()  + color2.getBlue())  % 256;
                
                Color color3 = new Color(red, green, blue);
                bfi3.setRGB(i, j, color3.getRGB());
            }
        }

        //Ajustes de Imagem
        Graphics g = bfi.getGraphics();


        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// Preenche o plano de fundo da pagina com branco
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, totalwidth, totalHeigh);
        //g.fillRect(0, 0, widthImg01, heightImg01);

        g.drawImage(img01, 0, 0, null);
        g.drawImage(img02, widthImg01, 0, null);
        g.drawImage(bfi3, widthImg01 + widthImg02, 0, null);
        // g.drawImage(bfi, widthImg01 + widthImg02, 0, null);


        //Fru Fru... adiciona uma linha prtea para separar as imagens
        g.setColor(Color.BLACK);
        g.fillRect(widthImg01, 0, 5, totalHeigh);

//Exibe o resultado final
        JFrame fr = new JFrame("Merge de imagens");
        fr.getContentPane().setLayout(new BorderLayout());
        JLabel jl = new JLabel(new ImageIcon(bfi));
        fr.getContentPane().add(jl, BorderLayout.CENTER);
        fr.pack();
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setVisible(true);
    }

//    private static BufferedImage createBufferedImage(Image image) {
//        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//        BufferedImage bm = null;
//        try {
//            // Determine the type of transparency of the new buffered image
//            int transparency = Transparency.OPAQUE;
////            if (hasAlpha) { transparency = Transparency.BITMASK; }
//            // Create the buffered image
//            GraphicsDevice gs = ge.getDefaultScreenDevice();
//            GraphicsConfiguration gc = gs.getDefaultConfiguration();
//            bm = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
//        } catch (HeadlessException e) {
//            // The system does not have a screen
//        }
//        return bm;
//    }
// This method returns a buffered image with the contents of an image 

    public static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }
        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();
        // Determine if the image has transparent pixels; for this method's
        // implementation, see Determining If an Image Has Transparent Pixels
        boolean hasAlpha = hasAlpha(image);
        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha) {
                transparency = Transparency.BITMASK;
            }
            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
            // The system does not have a screen
        }
        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }
        // Copy image to buffered image
        Graphics g = bimage.createGraphics();
        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bimage;
    }

    public static boolean hasAlpha(Image image) {

        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            return ((BufferedImage) image).getColorModel().hasAlpha();
        }

        // Use a pixel grabber to retrieve the image's color model;
        // grabbing a single pixel is usually sufficient
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);

        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }
        // Get the image's color model
        return pg.getColorModel().hasAlpha();
    }
}
Criado 27 de agosto de 2010
Ultima resposta 1 de set. de 2010
Respostas 3
Participantes 3