How to convert image to sepia in java?

£可爱£侵袭症+ 提交于 2020-01-04 03:48:45

问题


I'm looking for a free or toll library.

Update

It looks like there is no such a library, but the following code works like expected:

/**
*
* @param img Image to modify
* @param sepiaIntensity From 0-255, 30 produces nice results
* @throws Exception
*/
public static void applySepiaFilter(BufferedImage img, int sepiaIntensity) {
    // Play around with this. 20 works well and was recommended
    // by another developer. 0 produces black/white image
    int sepiaDepth = 20;

    int w = img.getWidth();
    int h = img.getHeight();

    WritableRaster raster = img.getRaster();

    // We need 3 integers (for R,G,B color values) per pixel.
    int[] pixels = new int[w*h*3];
    raster.getPixels(0, 0, w, h, pixels);

    // Process 3 ints at a time for each pixel.
    // Each pixel has 3 RGB colors in array
    for (int i=0;i<pixels.length; i+=3) {
        int r = pixels[i];
        int g = pixels[i+1];
        int b = pixels[i+2];

        int gry = (r + g + b) / 3;
        r = g = b = gry;
        r = r + (sepiaDepth * 2);
        g = g + sepiaDepth;

        if (r>255) r=255;
        if (g>255) g=255;
        if (b>255) b=255;

        // Darken blue color to increase sepia effect
        b-= sepiaIntensity;

        // normalize if out of bounds
        if (b<0) b=0;
        if (b>255) b=255;

        pixels[i] = r;
        pixels[i+1]= g;
        pixels[i+2] = b;
    }
    raster.setPixels(0, 0, w, h, pixels);
}

回答1:


http://www.swissdelphicenter.ch/en/showcode.php?id=1794

http://www.rhinocerus.net/forum/lang-java-programmer/574119-sepia-tone-image-filter-java.html




回答2:


It's possible to do this using the SepiaTone Effect class in the JavaFX library.



来源:https://stackoverflow.com/questions/5132015/how-to-convert-image-to-sepia-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!