喜欢拍照的友友们,肯定对各种修图、美颜、滤镜很感兴趣。今天我就简单地给大家讲一下图片原理和用Java编程实现的一些滤镜效果。
图片是个二维结构,有像素、分辨率、大小、颜色、色调、亮度等基本属性。其中像素个数就是图片的宽和高的乘积。我们要绘制图片,需要学会如何绘制像素点及如何设置颜色RGB。
// 绘制随机的一个颜色矩阵并存储起来
Random random = new Random ();
int[][] imgarr = new int[200][200];
for(int i = 0; i < 200; i++){
for(int j = 0; j < 200; j++){
// 随机一个(0-16777214)范围的整数
int randomRGB= random.nextInt (16777215);
imgarr[i][j]=randomRGB;
// 如何绘制一个像素点
Color color = new Color (randomRGB);
g.setColor (color);
g.fillRect (100+i,100+j,1,1);
}
}
// 遍历数组 取出所有的数设置为颜色再绘制出来
for(int i = 0; i < 200; i++){
for(int j=0;j<200;j++){
int rgb = imgarr[i][j];
Color color = new Color (rgb);
// int blue = (rgb>>0)&0xFF;
// 获取RGB三原色
int red = color.getRed ();
int green = color.getGreen ();
int blue = color.getBlue ();
Color nColor = new Color (red/2,green/2,blue/2);
g.setColor(nColor);
g.fillRect (350+i,100+j,1,1);
}
}
我们要对图片进行一些属性的修改,就需要获取有关图片数据,而图片数据就是int类型二维数组。所以当我们获取一张照片时,我们需要将其转为一个int类型的二维数组。那如何将一张照片转为一个int类型的二维数组呢?
/*给定一张照片 将其转为一个int类型的二维数组
@param imgpath 给定的照片路径
@param return1返回int类型二维数组*/
public int[][] getPixelArray(String imgPath){
// 根据图片地址 生成一个file对象
File file = new File (imgPath);
// 声明一个BUfferedImage变量名
BufferedImage buffimg = null;
// 使用图片IO读取文件
try {
buffimg = ImageIO.read (file);
}catch (IOException e) {
e.printStackTrace ();
}
int w = buffimg.getWidth();
int h = buffimg.getHeight();
// 定义一个与图片像素宽高一致的二维数组
int[][] imgarr = new int[w][h];
// 遍历BufferedImage 获取RGB值 存储 数组中
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
imgarr[i][j] = buffimg.getRGB (i, j);
}
}
//将存好像素值的数组 返回
return imgarr;
}
将照片转为int类型的二维数组后,我们需要遍历绘制所有的像素值。
//重新读取图片
int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
int w = imgarr.length;
int h = imgarr[0].length;
//绘制原图
// 遍历数组 取出所有的数设置为颜色再绘制出来
for(int i=0;i<w;i++) {
for(int j=0;j<h;j++) {
int rgb=imgarr[i][j];
Color color=new Color(rgb);
//获取RGB
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
Color ncolor=new Color(red,green,blue);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}
}
处理像素值,调整RGB颜色,实现多种滤镜效果
一灰度
int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
int w = imgarr.length;
int h = imgarr[0].length;
//图片色调设置为灰度
for(int i=0;i<w;i++) {
for(int j=0;j<h;j++) {
int rgb=imgarr[i][j];
Color color=new Color(rgb);
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int gray=(red+green+blue)/3;
Color ncolor=new Color(gray,gray,gray);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}
}
二黑白
int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
int w = imgarr.length;
int h = imgarr[0].length;
//图片色调设置为黑白
for(int i=0;i<w;i++) {
for(int j=0;j<h;j++) {
int rgb=imgarr[i][j];
Color color=new Color(rgb);
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int gray=(red+green+blue)/3;
if(gray<=100) {
Color ncolor=new Color(0,0,0);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}else {
Color ncolor=new Color(255,255,255);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}
}
}
三底片
int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
int w = imgarr.length;
int h = imgarr[0].length;
//图片色调设置为底片
for(int i=0;i<w;i++) {
for(int j=0;j<h;j++) {
int rgb=imgarr[i][j];
Color color=new Color(rgb);
int red=255-color.getRed();
int green=255-color.getGreen();
int blue=255-color.getBlue();
Color ncolor=new Color(red,green,blue);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}
}
完整的代码如下:
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;
import java.util.Random;
public class Imagepad extends JFrame {
public void init() {
this.setTitle("图像处理");
this.setSize(800,800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[]args) {
Imagepad imgpad=new Imagepad();
imgpad.init();
}
public int[][] getPixelArray(String imgPath){
// 根据图片地址 生成一个file对象
File file = new File (imgPath);
// 声明一个BUfferedImage变量名
BufferedImage buffimg = null;
// 使用图片IO读取文件
try {
buffimg = ImageIO.read (file);
}catch (IOException e) {
e.printStackTrace ();
}
int w = buffimg.getWidth();
int h = buffimg.getHeight();
// 定义一个与图片像素宽高一致的二维数组
int[][] imgarr = new int[w][h];
// 遍历BufferedImage 获取RGB值 存储 数组中
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
imgarr[i][j] = buffimg.getRGB (i, j);
}
}
//将存好像素值的数组 返回
return imgarr;
}
//重新读取图片
int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
int w = imgarr.length;
int h = imgarr[0].length;
public void paint(Graphics g) {
super.paint(g);
// 遍历数组 取出所有的数设置为颜色再绘制出来
for(int i=0;i<w;i++) {
for(int j=0;j<h;j++) {
int rgb=imgarr[i][j];
Color color=new Color(rgb);
//图片色调设置为灰度
/*int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int gray=(red+green+blue)/3;
Color ncolor=new Color(gray,gray,gray);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);*/
//图片色调设置为黑白,二值化
/*int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int gray=(red+green+blue)/3;
if(gray<=100) {
Color ncolor=new Color(0,0,0);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}else {
Color ncolor=new Color(255,255,255);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}*/
//图片滤镜设置为底片效果
int red=255-color.getRed();
int green=255-color.getGreen();
int blue=255-color.getBlue();
Color ncolor=new Color(red,green,blue);
g.setColor(ncolor);
g.fillRect(100+i,100+j,1,1);
}
}
}
}