透明度
介绍:
- Color 类用于封装默认 sRGB 颜色空间中的颜色,或者用于封装由 ColorSpace 标识的任意颜色空间中的颜色。每种颜色都有一个隐式的 alpha 值 1.0,或者有一个在构造方法中提供的显式的 alpha 值。alpha 值定义了颜色的透明度,可用一个在 0.0 - 1.0 或 0 - 255 范围内的浮点值表示它。alpha 值为 1.0 或 255 则意味着颜色完全是不透明的,alpha 值为 0 或 0.0 则意味着颜色是完全透明的。在使用显式的 alpha 值构造 Color 时,或者在获取某个 Color 的颜色/alpha 分量时,从不将颜色分量预乘 alpha 分量(复制java1.6API文档 Color类)
- alpha =0;代表完全透明 (看看透明图片部分效果跟带墨镜看差不多)
- alpha =255;完全不透明 (跟原图一样)
- alpha 透明度可以修改,改了之后还能恢复原样(在做中软杯项目文字识别不知道
怎么了,恢复不了原图白色背景,允许是图片本来就是黑色背景,不是白色背景) - 注意:很多透明化处理的图片时,原图片背景一般是黑色。
实现工具类:
- java调整图片透明度,需要保存成png格式。透明度输入参数alpha取值0-255
- 关键代码
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,也就是将整个图像呈现出明显的只有黑和白的视觉效果
同:可能会失真,有些有用的信息会丢掉,二值化与灰度化主要丢了没有用的信息,之后简化图片
实现二值化与灰度化工具类
- tess4j,有自己的图片处理方法 Class ImageHelper;具体看开发文档http://tess4j.sourceforge.net/docs/docs-4.0/
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();
}