Java 图片处理

import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;

public class Main { private static Color colorOfSearch = Color.decode("#133c74"); private static Color colorOfBSearch = Color.decode("#2c3b34"); private static int sim=10; public static void main(String[] args) {

    BufferedImage file = getImage("cccc.jpeg");
    System.out.println("width:" + file.getWidth() + "   height:" + file.getHeight());
    //int[] pixels=new int[file.getWidth()*file.getHeight()];
    int[][] array = converImageToArray(file);
    removeLine(array, file.getWidth(), file.getHeight());

    int[] pixels = dArrayToOne(array, file.getWidth(), file.getHeight());

    setRGB(file, pixels);
    System.out.println("array width:" + array.length);

    writeToFile(file, "bbb.png");
    //System.out.println(Arrays.toString(array[file.getWidth()-1]));
}

public static void generateNoiseImage(int[][] piexls, int width, int height) {
    int index = 0;
    int a = 255;
    int r = 0;
    int g = 0;
    int b = 0;

    for (int row = 0; row < 100; row++) {
        for (int col = 0; col < 100; col++) {
            // set random color value for each pixel
            r = (int) (128.0 + (128.0 * Math.sin((row + col) / 8.0)));
            g = (int) (128.0 + (128.0 * Math.sin((row + col) / 8.0)));
            b = (int) (128.0 + (128.0 * Math.sin((row + col) / 8.0)));

            piexls[row][col] = ((clamp(a) & 0xff) << 24) |
                    ((clamp(r) & 0xff) << 16) |
                    ((clamp(g) & 0xff) << 8) |
                    ((clamp(b) & 0xff));
            //piexls[row][col]=0xff<<24 | 0xff<<16 | 0xff <<8 | 0xff;
            index++;
        }
    }

}

/***
public static void removeLine(int[][] piexls, int width, int height) {
    int index = 0;
    int a = 255;
    int r = 0;
    int g = 0;
    int b = 0;

    for (int row = 0; row < width; row++) {
        for (int col = 0; col < height; col++) {
            int colorOfPixcel = piexls[row][col];
            Color color = new Color(colorOfPixcel);
            if (isMatch(color, colorOfSearch, sim)) {
                    piexls[row][col]=0xff<<24 | 0xff<<16 | 0xff <<8 | 0xff;;//getAvgColor(piexls, row, col,width, 100);

            }
            index++;
        }
    }

}***/
public static void removeLine(int[][] piexls, int width, int height) {
    int index = 0;
    int a = 255;
    int r = 0;
    int g = 0;
    int b = 0;

    for (int row = 0; row < width; row++) {
        for (int col = 0; col < height; col++) {
            int colorOfPixcel = piexls[row][col];
            Color color = new Color(colorOfPixcel);
            if (isMatch(color, colorOfSearch, sim) || isMatch(color, colorOfBSearch, sim)) {
                //for(int i=0;i<width;i++){
                    piexls[row][col]=getAvgColor(piexls, row, col,width, 100);
                //}

            }
            index++;
        }
    }

}

private static boolean isMatch(Color color, Color colorOfSearch, int sim) {
    if (Math.abs(color.getAlpha() - colorOfSearch.getAlpha()) <= sim && Math.abs(color.getRed() - colorOfSearch.getRed()) <= sim && Math.abs(color.getGreen() - colorOfSearch.getGreen()) <= sim && Math.abs(color.getBlue() - colorOfSearch.getBlue()) <= sim) {
        return true;
    } else {
        return false;
    }
}


private static int getAvgColor(int[][] array, int x, int y,int width, int size) {

    int totalRed = 0;
    int totalGreen = 0;
    int totalBlue = 0;
    int totalSize=0;
    for (int i = -size; i < size; i++) {
        for(int j=-size;j<size;j++){
            if (x-i>0 && (x-i<width) && (y - j) > 0 &&(y-j<array.length) ) {
            // ((y - j) > 0 &&(y-j<array.length) ) {
                Color color = new Color(array[x][y - j]);
                if(!isMatch(color,colorOfSearch,sim)){
                    totalRed += color.getRed();
                    totalGreen += color.getGreen();
                    totalBlue += color.getBlue();
                    totalSize++;
                }
                //System.out.println("color red:"+color.getRed()+"color greed:"+color.getGreen()+"color blue:"+color.getBlue()+" totalSize:"+totalSize);
            }
        }

    }
    //System.out.println(totalRed+" r "+totalGreen+" g "+totalBlue+" b "+totalSize);
    if(totalSize>0){
        Color color=new Color(totalRed/totalSize,totalGreen/totalSize,totalBlue/totalSize);
        return color.getRGB();
    }else{
        return Color.black.getRGB();
    }

}

private static int clamp(int rgb) {
    if (rgb > 255)
        return 255;
    if (rgb < 0)
        return 0;
    return rgb;
}


public static BufferedImage getImage(String filePath) {
    File file = new File(filePath);

    try {
        BufferedImage image = ImageIO.read(file);
        return image;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}


public static int[] getRGB(BufferedImage image, int[] pixels) {
    int type = image.getType();
    if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
        return (int[]) image.getRaster().getDataElements(0, 0, image.getWidth(), image.getHeight(), pixels);
    else
        return image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
}


public static void setRGB(BufferedImage image, int[] pixels) {
    int type = image.getType();
    if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
        image.getRaster().setDataElements(0, 0, image.getWidth(), image.getHeight(), pixels);
    else
        image.setRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
}

public static int[][] converImageToArray(BufferedImage image) {
    int[] pixels = getRGB(image, new int[image.getWidth() * image.getHeight()]);

    int[][] array = new int[image.getWidth()][image.getHeight()];
    System.out.println("pixels size:" + pixels.length);
    for (int i = 0; i < pixels.length; i++) {
        int x = i % image.getWidth();
        int y = i / image.getWidth();
        array[x][y] = pixels[i];
    }

    return array;
}


public static int[] dArrayToOne(int[][] array, int width, int height) {
    int[] pixels = new int[width * height];
    System.out.println("new array width:" + width + "   height:" + height);
    int index = 0;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            pixels[index] = array[j][i];
            index++;
        }
    }
    return pixels;
}


public static void writeToFile(BufferedImage file, String outPath) {
    try {
        ImageIO.write(file, "png", new File(outPath));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

转载于:https://my.oschina.net/fengcunhan/blog/811357

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值