一,图像原理
图像是由一个个像素点组成的矩阵形成的,每个像素点的大小是1 int(32bit),4个字节分别对应A,R,G,B (0-255byte)
0000 0000 A
0000 0000 R
0000 0000 G
0000 0000 B
1,如何将一个int拆分成4个byte
通过>>和&
位运算: int num = 167000151;
int a = (num >> 24) & 255; int r = (num >> 16) & 255; int g = (num >> 8) & 255; int b = (num>>0) & 255;
2,如何将4个byte和成1个int
int argb = a<<24 | r<<16 | g<<8 | b;
3,运算符
&:若干个条件为true,最终为true
|:若干个条件有一个为true,最终为true
!:true变false,false变true
二,图像处理
原图
1,马赛克
输入参数为马赛克区域边长,每隔mSize取一格算出平均值填充。
public void drawMasic(Graphics g,int mSize){
BufferedImage buffimage = new BufferedImage(imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i> 16)&255;
green += (pixelnum >> 8)&255;
blue += (pixelnum >> 0)&255;
//int gray = (red+green+blue)/3;
}
}
red/=100;
green/=100;
blue/=100;
int newPixel = 255<<24|red<<16|green<<8|blue;
for (int k = 0; k < mSize; k++) {
for(int l = 0; l < mSize; l++){
buffimage.setRGB (i+k,j+l,newPixel);
}
}
}
}
g.drawImage(buffimage,30,50,null);
}
2,旋转180
用矩阵的长和宽减去各个元素原先的坐标得出的是旋转180后的坐标。
public void xuanZhuan180(Graphics g){
int after [][]=new int[imgArray.length][imgArray[0].length];
BufferedImage buffimage = new BufferedImage (imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length; j++) {
after[i][j]=imgArray[imgArray.length-1-i][imgArray[0].length-1-j];
}
}
for(int i = 0; i < after.length; i++) {
for (int j = 0; j < after[i].length; j++) {
int pixelnum = after[i][j];
int red = (pixelnum >> 16)&255;
int green = (pixelnum >> 8)&255;
int blue = (pixelnum >> 0)&255;
int grayPixel = 255<<24|red<<16|green<<8|blue<<0;
buffimage.setRGB(i,j,grayPixel);
}
}
g.drawImage (buffimage, 30, 50, null);
}
3,黑白两色
算出r,g,b平均值令为gray,灰色小于150令为黑色,大于150令为白色。
public void blackWhite(Graphics g){
BufferedImage buffimage = new BufferedImage (imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imgArray.length; i++){
for(int j = 0; j < imgArray[i].length; j++){
int pixelnum = imgArray[i][j];
int red = (pixelnum >> 16)&255;
int green = (pixelnum >> 8)&255;
int blue = (pixelnum >> 0)&255;
int gray = (red+green+blue)/3;
Color color = new Color(gray,gray,gray);
if(gray<150){
g.setColor(Color.BLACK);
}else{
g.setColor(Color.WHITE);
}
g.fillRect(i,j,1,1);
}
}
}
4,灰白照
算出r,g,b平均值令为gray,三种颜色用灰色填充。
public void drawSrcImage(Graphics g){
BufferedImage buffimage = new BufferedImage (imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imgArray.length; i++){
for(int j = 0; j < imgArray[i].length; j++){
int pixelnum = imgArray[i][j];
int red = (pixelnum >> 16)&255;
int green = (pixelnum >> 8)&255;
int blue = (pixelnum >> 0)&255;
int gray = (red+green+blue)/3;
int grayPixel = 255<<24|gray<<16|gray<<8|gray<<0;
//Color color = new Color (gray,gray,gray);
//g.setColor(color);
//g.fillRect(i+30,j+50,1,1);
buffimage.setRGB(i,j,grayPixel);
}
}
g.drawImage (buffimage, 30, 50, null);
}
5,加深R,G,B其中一种颜色
在人r,g,b原有的数值上加一定的数,大于255就定为255。
public void drawSrcImage(Graphics g){
BufferedImage buffimage = new BufferedImage (imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imgArray.length; i++){
for(int j = 0; j < imgArray[i].length; j++){
int pixelnum = imgArray[i][j];
int red = (pixelnum >> 16)&255;
int green = (pixelnum >> 8)&255;
int blue = (pixelnum >> 0)&255;
int gray = (red+green+blue)/3;
int newred = red+100;
if(newred>255){
newred = 255;
}
int newgreen = red+80;
if(newgreen>255){
newgreen = 255;
}
int newblue = red+50;
if(newblue>255) {
newblue = 255;
}
int grayPixel = 255<<24|red<<16|green<<8|newblue<<0;
//Color color = new Color (gray,gray,gray);
//g.setColor(color);
//g.fillRect(i+30,j+50,1,1);
buffimage.setRGB(i,j,grayPixel);
}
}
g.drawImage (buffimage, 30, 50, null);
}
(1)加深blue
(2)加深green
(3)加深red
三,完整代码
package java_study;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageProUI extends JFrame {
int[][] imgArray;
{// 代码块
imgArray = getImageArray
("C:\\Users\\18384\\Desktop\\zzz.jpg");
System.out.println ("图片转化完成!");
}
public void paint(Graphics g){
super.paint(g);
//drawMasic(g,20);
drawSrcImage(g);
//xuanZhuan180(g);
//blackWhite(g);
}
public void drawMasic(Graphics g,int mSize){
BufferedImage buffimage = new BufferedImage(imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i> 16)&255;
green += (pixelnum >> 8)&255;
blue += (pixelnum >> 0)&255;
//int gray = (red+green+blue)/3;
}
}
red/=100;
green/=100;
blue/=100;
int newPixel = 255<<24|red<<16|green<<8|blue;
for (int k = 0; k < mSize; k++) {
for(int l = 0; l < mSize; l++){
buffimage.setRGB (i+k,j+l,newPixel);
}
}
}
}
g.drawImage(buffimage,30,50,null);
}
public void drawSrcImage(Graphics g){
BufferedImage buffimage = new BufferedImage (imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imgArray.length; i++){
for(int j = 0; j < imgArray[i].length; j++){
int pixelnum = imgArray[i][j];
int red = (pixelnum >> 16)&255;
int green = (pixelnum >> 8)&255;
int blue = (pixelnum >> 0)&255;
int gray = (red+green+blue)/3;
int newred = red+50;
if(newred>255){
newred = 255;
}
int newgreen = red+50;
if(newgreen>255){
newgreen = 255;
}
int newblue = red+50;
if(newblue>255) {
newblue = 255;
}
int grayPixel = 255<<24|newred<<16|green<<8|blue<<0;
//Color color = new Color (gray,gray,gray);
//g.setColor(color);
//g.fillRect(i+30,j+50,1,1);
buffimage.setRGB(i,j,grayPixel);
}
}
g.drawImage (buffimage, 30, 50, null);
}
public void xuanZhuan180(Graphics g){
int after [][]=new int[imgArray.length][imgArray[0].length];
BufferedImage buffimage = new BufferedImage (imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length; j++) {
after[i][j]=imgArray[imgArray.length-1-i][imgArray[0].length-1-j];
}
}
for(int i = 0; i < after.length; i++) {
for (int j = 0; j < after[i].length; j++) {
int pixelnum = after[i][j];
int red = (pixelnum >> 16)&255;
int green = (pixelnum >> 8)&255;
int blue = (pixelnum >> 0)&255;
int grayPixel = 255<<24|red<<16|green<<8|blue<<0;
buffimage.setRGB(i,j,grayPixel);
}
}
g.drawImage (buffimage, 30, 50, null);
}
public void blackWhite(Graphics g){
BufferedImage buffimage = new BufferedImage (imgArray.length,
imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i < imgArray.length; i++){
for(int j = 0; j < imgArray[i].length; j++){
int pixelnum = imgArray[i][j];
int red = (pixelnum >> 16)&255;
int green = (pixelnum >> 8)&255;
int blue = (pixelnum >> 0)&255;
int gray = (red+green+blue)/3;
Color color = new Color(gray,gray,gray);
if(gray<150){
g.setColor(Color.BLACK);
}else{
g.setColor(Color.WHITE);
}
g.fillRect(i,j,1,1);
}
}
}
public int[][] getImageArray(String path){
File file = new File (path);
BufferedImage buffimg = null;
try {
buffimg = ImageIO.read(file);
}catch(IOException e){
throw new RuntimeException(e);
}
int w = buffimg.getWidth ();
int h = buffimg.getHeight ();
int[][] imgArray = new int[w][h];
for(int i=0; i < w; i++){
for(int j=0; j < h; j++){
int rgb = buffimg.getRGB(i,j);
imgArray[i][j] = rgb;
}
}
return imgArray;
}
public static void main(String[] args) {
ImageProUI imageProUI = new ImageProUI();
imageProUI.setSize(800, 600);
imageProUI.setTitle("图像处理");
imageProUI.setDefaultCloseOperation(EXIT_ON_CLOSE);
imageProUI.setVisible(true);
}
}