java图像增强_java图片对比度调整示例代码
前言
本文主要給大家介紹了關于java圖片對比度調整的方法,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧
方法如下
測試代碼
public static void main(String[] args) {
//文件與BufferedImage間的轉換
BufferedImage bi=file2img("test.jpg"); //讀取圖片
BufferedImage bii=img_color_contrast(bi,100);
img2file(bii,"jpg","test1.jpg"); //生成圖片
}
圖片對比度調整代碼
//圖片對比度調整
public static BufferedImage img_color_contrast(BufferedImage imgsrc, int contrast) {
try {
int contrast_average = 128;
//創建一個不帶透明度的圖片
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;
}
}
圖片讀取,和存儲函數
//讀取圖片
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();
}
}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
總結
以上是生活随笔為你收集整理的java图像增强_java图片对比度调整示例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 字符串 面试_Java 字符串
- 下一篇: java map赋值_java 中的ma