BufferedImage back=new BufferedImage(imgsrc.getWidth(), imgsrc.getHeight(), BufferedImage.TYPE_INT_ARGB);
Color newcolor = new Color(color.getRed(), color.getGreen(),color.getBlue(), alpha);
back.setRGB(i,j,newcolor.getRGB());
public static void main(String[] args) {
//文件与BufferedImage间的转换
BufferedImage bi=file2img("test.jpg"); //读取图片
BufferedImage bii=img_alpha(bi,150);
img2file(bii,"PNG","test1.png"); //生成图片
}
图片透明度更换函数
public static BufferedImage img_alpha(BufferedImage imgsrc,int alpha) {
try {
//创建一个包含透明度的图片,半透明效果必须要存储为png合适才行,存储为jpg,底色为黑色
BufferedImage back=new BufferedImage(imgsrc.getWidth(), imgsrc.getHeight(), BufferedImage.TYPE_INT_ARGB);
int width = imgsrc.getWidth();
int height = imgsrc.getHeight();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
int rgb = imgsrc.getRGB(i, j);
Color color = new Color(rgb);
Color newcolor = new Color(color.getRed(), color.getGreen(),color.getBlue(), alpha);
back.setRGB(i,j,newcolor.getRGB());
}
}
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();
}
}
灰度化:在RGB模型中,如果R=G=B时,则彩色表示一种灰度颜色,其中R=G=B的值叫灰度值,因此,灰度图像每个像素只需一个字节存放灰度值(又称强度值、亮度值),灰度范围为0-255。一般常用的是加权平均法来获取每个像素点的灰度值。
二值化:图像的二值化,就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的只有黑和白的视觉效果
同:可能会失真,有些有用的信息会丢掉,二值化与灰度化主要丢了没有用的信息,之后简化图片
static java.awt.image.BufferedImage convertImage2Binary(java.awt.image.BufferedImage image)
Deprecated.
As of release 1.1, renamed to convertImageToBinary(BufferedImage image)
static java.awt.image.BufferedImage convertImageToBinary(java.awt.image.BufferedImage image)
A simple method to convert an image to binary or B/W image.
static java.awt.image.BufferedImage convertImageToGrayscale(java.awt.image.BufferedImage image)
A simple method to convert an image to gray scale.
package image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageDemo {
public void binaryImage() throws IOException{
File file = new File(System.getProperty("user.dir")+"/src/2722425974762424026.jpg");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_BINARY
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = image.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
File newFile = new File(System.getProperty("user.dir")+"/src/2722425974762424028.jpg");
ImageIO.write(grayImage, "jpg", newFile);
}
public void grayImage() throws IOException{
File file = new File(System.getProperty("user.dir")+"/src/2722425974762424026.jpg");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_GRAY
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = image.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
File newFile = new File(System.getProperty("user.dir")+"/src/2722425974762424027.jpg");
ImageIO.write(grayImage, "jpg", newFile);
}
public static void main(String[] args) throws IOException {
ImageDemo demo = new ImageDemo();
demo.binaryImage();
demo.grayImage();
}
}
public static void toMoveShuiying(String srcPath, String descPath) throws IOException {
// 定义一个RGB的数组,因为图片的RGB模式是由三个 0-255来表示的 比如白色就是(255,255,255)
int[] rgb = new int[3];
// 用来处理图片的缓冲流
BufferedImage bi = ImageIO.read(new File(srcPath));
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j);
// 分别进行位操作得到 r g b上的值
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
if (rgb[0] == 0 || rgb[0] == 229) {
// -16777216 黑色 pixel=-1白
Color color = new Color(255, 255, 255, 255);
bi.setRGB(i, j, color.getRGB());
}
}
}
FileOutputStream ops = new FileOutputStream(descPath);
ImageIO.write(bi, "png", ops);
ops.flush();
ops.close();
}