No se puede obtener la escala de grises de una imagen en java

Baran Sahin:

Tengo un problema con la obtención de la escala de grises de un archivo .jpg. Estoy tratando de crear un nuevo archivo .jpg como escala de grises pero estoy simplemente copiando la imagen nada más. Aquí está mi código:

package training01;

import java.awt.*;
import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class GrayScale {
    BufferedImage image;
    int width;
    int height;
    public GrayScale() {
        try {
            File input = new File("digital_image_processing.jpg");
            image = ImageIO.read(input);
            width = image.getWidth();
            height = image.getHeight();
            for(int i = width;i < width;i++) {
                for(int j = height;j < height;j++) {
                    Color c =  new Color(image.getRGB(i, j));
                    int red = c.getRed();
                    int green = c.getGreen();
                    int blue = c.getBlue();
                    int val = (red+green+blue)/3;
                    Color temp = new Color(val,val,val);
                    image.setRGB(i, j, temp.getRGB());
                }
            }
            File output = new File("digital_image_processing1.jpg");
            ImageIO.write(image, "jpg", output);
        }catch(Exception e) {
            System.out.println(e);
        }
    }
    public static void main(String[] args) {
        GrayScale gs = new GrayScale();
    }
}
WJS:

Es necesario cambiar la siguiente. Comience su iyj a 0.

      for(int i = width;i < width;i++) {  
         for(int j = height;j < height;j++) {

Sin embargo, aquí hay una manera más rápida de hacerlo. Escribirla en un nuevo objeto BufferedImage que se establece para la escala de grises.

      image = ImageIO.read(input);
      width = image.getWidth();
      height = image.getHeight();
      bwImage = new BufferedImage(width,
            height, BufferedImage.TYPE_BYTE_GRAY);
      Graphics g = bwImage.getGraphics();
            g.drawImage(image,0,0,null);

A continuación, guarde el bwImage.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=370959&siteId=1
Recomendado
Clasificación