imageoperations.java

来自「JAVA 開發的看圖軟件.可以自由開啟圖像,而家可以加入冷色,暖色,對比色等較果」· Java 代码 · 共 96 行

JAVA
96
字号
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.LookupOp;
import java.awt.image.ShortLookupTable;
/*
 * ImageOperations.java
 *
 * Created on November 30, 2007, 11:17 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author s0659879
 */
public class ImageOperations {
    
    
    private static short[] red = new short[256];
    private static short[] green = new short[256];
    private static short[] blue = new short[256];
    
    public static BufferedImage warm (BufferedImage image) {
        
        for (int i = 0; i < 256; i++) {
            red[i] = (short) (i + 15);
            green[i] = (short) i;
            blue[i] = (short) i;
            if (red[i] > 255)
                red[i] = 255;
            }
        short[][] warm = new short[][] {red, green, blue};
        BufferedImageOp op = new LookupOp(new ShortLookupTable(0, warm), null);
        BufferedImage oimage = op.filter(image, null);
        return oimage;
   }
    
    public static BufferedImage chill (BufferedImage image) {
        
        for (int i = 0; i < 256; i++) {
            red[i] = (short) i;
            green[i] = (short) i;
            blue[i] = (short) (i + 15);
            if (blue[i] > 255)
                blue[i] = 255;
            }
        short[][] chill = new short[][] {red, green, blue};
        BufferedImageOp op = new LookupOp(new ShortLookupTable(0, chill), null);
        BufferedImage oimage = op.filter(image, null);
        return oimage;
   }
    
        public static BufferedImage negation (BufferedImage image) {
        
        for (int i = 0; i < 256; i++) {
            red[i] = (short) (255 - i);
            green[i] = (short) (255 - i);
            blue[i] = (short) (255 - i);
            
            }
        short[][] negation = new short[][] {red, green, blue};
        BufferedImageOp op = new LookupOp(new ShortLookupTable(0, negation), null);
        BufferedImage oimage = op.filter(image, null);
        return oimage;
   }

    
    public static BufferedImage posterize (BufferedImage image, int level) {
        for (int i = 0; i<256; i++){
            red[i] = (short) ((i>> level) << level);
            green[i] = (short) ((i>> level) << level);
            blue[i] = (short) ((i>> level) << level);
        }
        short[][] posterize = new short[][] {red, green, blue};
        BufferedImageOp op = new LookupOp(new ShortLookupTable(0, posterize), null);
        BufferedImage oimage = op.filter(image, null);
        return oimage;
    }
    
        public static BufferedImage blurKernel (BufferedImage image) {
            float ninth = 1.0f / 9.0f;
            float[] blurKernel = {
                ninth, ninth, ninth,
                ninth, ninth, ninth,
                ninth, ninth, ninth,
                };
            BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, blurKernel));
            BufferedImage oimage = op.filter(image, null);
            return oimage;
            }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?