📄 grayimage.java
字号:
package image;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.awt.image.Raster;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class GrayImage extends BasicImage {
Image imageProcessed=null;
public GrayImage() {
super();
// TODO 自动生成构造函数存根
}
public GrayImage(String fileName) {
super(fileName);
// TODO 自动生成构造函数存根
}
public int graysinglepixel(int x, int y, int pixel) {//读取 alpha red green blue 的值
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
int gray=(int)( 0.30*red+0.59*green+0.11*blue);
return gray;//Gray=0.30*R+0.59*G+0.11*B
// Deal with the pixel as necessary...
}
public int[] grayImage(Image img, int x, int y, int w, int h)
{
int[] gray_data = this.imageData;
//int [] gray_data=new int[w * h];
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int gray=graysinglepixel(x+i, y+j, gray_data[j * w + i]);
gray_data[w*j+i]=(255<<24)|(gray<<16)|(gray<<8)|gray;
//gray_data[j * w + i]=cell_gray_value;
}
}
return gray_data;
}
public int[] grayImage()
{
System.out.println("-------------grayImage()");
return this.grayImage(image,0,0,this.wideth,this.height);
}
public Image imageProcess()
{
System.out.println("-------------imageProcess()");
int [] data_gray= this.grayImage();
imageProcessed=this.createImage(data_gray);
return imageProcessed;
}
public void saveImageAfterProcess()
{
System.out.println("-------------imageProcess()");
this.saveImage("d:\\006.bmp");
}
public void showImageAfterProcess()
{}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
GrayImage zyy=new GrayImage("003.jpg");
zyy.imageProcess();
zyy.saveImage("d:\\007.bmp");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -