BufferImage emboss : BufferedImage « java.awt.image « Java by API






BufferImage emboss

 
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

  public void paint(Graphics g) {
    BufferedImage clone = emboss(createImage());
    
    g.drawImage(clone, 20,20,this);
  }

  public BufferedImage emboss(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dst;
    dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

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

        if (i > 0 && j > 0)
          upperLeft = src.getRGB(j - 1, i - 1);

        if (i < height - 1 && j < width - 1)
          lowerRight = src.getRGB(j + 1, i + 1);

        int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255);

        int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255);

        int blueDiff = (lowerRight & 255) - (upperLeft & 255);

        int diff = redDiff;
        if (Math.abs(greenDiff) > Math.abs(diff))
          diff = greenDiff;
        if (Math.abs(blueDiff) > Math.abs(diff))
          diff = blueDiff;

        int grayColor = 128 + diff;

        if (grayColor > 255)
          grayColor = 255;
        else if (grayColor < 0)
          grayColor = 0;

        int newColor = (grayColor << 16) + (grayColor << 8) + grayColor;

        dst.setRGB(j, i, newColor);
      }

    return dst;
  }
  
  private BufferedImage createImage(){
    BufferedImage bufferedImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.getGraphics();
    g.drawString("www.java2s.com", 20,20);
    
    return bufferedImage;
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
  
  
}
           
         
  








Related examples in the same category

1.BufferedImage.TYPE_INT_RGB
2.new BufferedImage(int width, int height, int imageType)
3.BufferedImage: getSource()
4.BufferedImage: setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)