java图像增强_java图片对比度调整示例代码
前言
本文主要給大家介紹了關(guān)于java圖片對比度調(diào)整的方法,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧
方法如下
測試代碼
public static void main(String[] args) {
//文件與BufferedImage間的轉(zhuǎn)換
BufferedImage bi=file2img("test.jpg"); //讀取圖片
BufferedImage bii=img_color_contrast(bi,100);
img2file(bii,"jpg","test1.jpg"); //生成圖片
}
圖片對比度調(diào)整代碼
//圖片對比度調(diào)整
public static BufferedImage img_color_contrast(BufferedImage imgsrc, int contrast) {
try {
int contrast_average = 128;
//創(chuàng)建一個(gè)不帶透明度的圖片
BufferedImage back=new BufferedImage(imgsrc.getWidth(), imgsrc.getHeight(),BufferedImage.TYPE_INT_RGB);
int width = imgsrc.getWidth();
int height = imgsrc.getHeight();
int pix;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pixel = imgsrc.getRGB(j, i);
Color color = new Color(pixel);
if (color.getRed() < contrast_average)
{
pix = color.getRed()- Math.abs(contrast);
if (pix < 0) pix = 0;
}
else
{
pix = color.getRed() + Math.abs(contrast);
if (pix > 255) pix = 255;
}
int red= pix;
if (color.getGreen() < contrast_average)
{
pix = color.getGreen()- Math.abs(contrast);
if (pix < 0) pix = 0;
}
else
{
pix = color.getGreen() + Math.abs(contrast);
if (pix > 255) pix = 255;
}
int green= pix;
if (color.getBlue() < contrast_average)
{
pix = color.getBlue()- Math.abs(contrast);
if (pix < 0) pix = 0;
}
else
{
pix = color.getBlue() + Math.abs(contrast);
if (pix > 255) pix = 255;
}
int blue= pix;
color = new Color(red,green,blue);
int x=color.getRGB();
back.setRGB(j,i,x);
}
}
return back;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
圖片讀取,和存儲函數(shù)
//讀取圖片
public static BufferedImage file2img(String imgpath) {
try {
BufferedImage bufferedImage=ImageIO.read(new File(imgpath));
return bufferedImage;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//保存圖片,extent為格式,"jpg"、"png"等
public static void img2file(BufferedImage img,String extent,String newfile) {
try {
ImageIO.write(img, extent, new File(newfile));
} catch (Exception e) {
e.printStackTrace();
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。
總結(jié)
以上是生活随笔為你收集整理的java图像增强_java图片对比度调整示例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 字符串 面试_Java 字符串
- 下一篇: mysql opaq数据库_MySql数