日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java图片透明化操作

發布時間:2024/1/18 java 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java图片透明化操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class ClearImageHelper {
public static BufferedImage cleanImage(BufferedImage bufferedImage)throws IOException{??
? ? ? ? int h = bufferedImage.getHeight();??
? ? ? ? int w = bufferedImage.getWidth();??
??
? ? ? ? // 灰度化??
? ? ? ? int[][] gray = new int[w][h];??
? ? ? ? for (int x = 0; x < w; x++){??
? ? ? ? ? ? for (int y = 0; y < h; y++){??
? ? ? ? ? ? ? ? int argb = bufferedImage.getRGB(x, y);??
? ? ? ? ? ? ? ? // 圖像加亮(調整亮度識別率非常高)??
? ? ? ? ? ? ? ? int r = (int) (((argb >> 16) & 0xFF) * 1.1 + 30);??
? ? ? ? ? ? ? ? int g = (int) (((argb >> 8) & 0xFF) * 1.1 + 30);??
? ? ? ? ? ? ? ? int b = (int) (((argb >> 0) & 0xFF) * 1.1 + 30);??
? ? ? ? ? ? ? ? if (r >= 255){??
? ? ? ? ? ? ? ? ? ? r = 255;??
? ? ? ? ? ? ? ? }??
? ? ? ? ? ? ? ? if (g >= 255){??
? ? ? ? ? ? ? ? ? ? g = 255;??
? ? ? ? ? ? ? ? }??
? ? ? ? ? ? ? ? if (b >= 255){??
? ? ? ? ? ? ? ? ? ? b = 255;??
? ? ? ? ? ? ? ? }??
? ? ? ? ? ? ? ? gray[x][y] = (int) Math.pow((Math.pow(r, 2.2) * 0.2973 + Math.pow(g, 2.2)* 0.6274 + Math.pow(b, 2.2) * 0.0753), 1 / 2.2);??
? ? ? ? ? ? }??
? ? ? ? }??
??
? ? ? ? // 二值化??
? ? ? ? int threshold = ostu(gray, w, h);??
? ? ? ? BufferedImage binaryBufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_BYTE_BINARY);??
? ? ? ? for (int x = 0; x < w; x++){??
? ? ? ? ? ? for (int y = 0; y < h; y++){??
? ? ? ? ? ? ? ? if (gray[x][y] > threshold){??
? ? ? ? ? ? ? ? ? ? gray[x][y] |= 0x00FFFF;??
? ? ? ? ? ? ? ? } else{??
? ? ? ? ? ? ? ? ? ? gray[x][y] &= 0xFF0000;??
? ? ? ? ? ? ? ? }??
? ? ? ? ? ? ? ? binaryBufferedImage.setRGB(x, y, gray[x][y]);??
? ? ? ? ? ? }??
? ? ? ? }??
??
? ? ? ? return binaryBufferedImage;
? ? }??
public static int ostu(int[][] gray, int w, int h){??
? ? ? ? int[] histData = new int[w * h];??
? ? ? ? // Calculate histogram??
? ? ? ? for (int x = 0; x < w; x++){??
? ? ? ? ? ? for (int y = 0; y < h; y++){??
? ? ? ? ? ? ? ? int red = 0xFF & gray[x][y];??
? ? ? ? ? ? ? ? histData[red]++;??
? ? ? ? ? ? }??
? ? ? ? }??
??
? ? ? ? // Total number of pixels??
? ? ? ? int total = w * h;??
??
? ? ? ? float sum = 0;??
? ? ? ? for (int t = 0; t < 256; t++)??
? ? ? ? ? ? sum += t * histData[t];??
??
? ? ? ? float sumB = 0;??
? ? ? ? int wB = 0;??
? ? ? ? int wF = 0;??
??
? ? ? ? float varMax = 0;??
? ? ? ? int threshold = 0;??
??
? ? ? ? for (int t = 0; t < 256; t++){??
? ? ? ? ? ? wB += histData[t]; // Weight Background??
? ? ? ? ? ? if (wB == 0)??
? ? ? ? ? ? ? ? continue;??
??
? ? ? ? ? ? wF = total - wB; // Weight Foreground??
? ? ? ? ? ? if (wF == 0)??
? ? ? ? ? ? ? ? break;??
??
? ? ? ? ? ? sumB += (float) (t * histData[t]);??
??
? ? ? ? ? ? float mB = sumB / wB; // Mean Background??
? ? ? ? ? ? float mF = (sum - sumB) / wF; // Mean Foreground??
??
? ? ? ? ? ? // Calculate Between Class Variance??
? ? ? ? ? ? float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);??
??
? ? ? ? ? ? // Check if new maximum found??
? ? ? ? ? ? if (varBetween > varMax){??
? ? ? ? ? ? ? ? varMax = varBetween;??
? ? ? ? ? ? ? ? threshold = t;??
? ? ? ? ? ? }??
? ? ? ? }??
??
? ? ? ? return threshold;??
? ? }??
? ? //圖片灰度,黑白??
? ? public static BufferedImage gray(BufferedImage src) {??
? ? ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);??
? ? ColorConvertOp op = new ColorConvertOp(cs, null);??
? ? src = op.filter(src, null);??
? ? ? ? return src;
? ? }??
? ? /**?
? ? * 設置源圖片為背景透明,并設置透明度?
? ? * @param srcImageFile 源圖片?
? ? * @param alpha 透明度 (0-10依次不透明)
? ? */??
public static BufferedImage transparentImage(String? srcImageFile,int alpha) {??
? ? BufferedImage bufferedImage=null;
? ? try {??
? ? //讀取圖片??
? ? FileInputStream stream = new FileInputStream(new File(srcImageFile));// 指定要讀取的圖片??
? ? // 定義一個字節數組輸出流,用于轉換數組??
? ? ByteArrayOutputStream os = new ByteArrayOutputStream();??
? ?
? ? byte[] data =new byte[1024];// 定義一個1K大小的數組??
? ? while (stream.read(data) != -1) {??
? ? os.write(data);??
? ? }??
??
? ? ? ? ImageIcon imageIcon = new ImageIcon(os.toByteArray());??
? ? ? ? bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),??
? ? ? ? BufferedImage.TYPE_4BYTE_ABGR);
? ? ? ? Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();??
? ? ? ? g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());??
??
? ? ? ? //判讀透明度是否越界??
? ? ? ? if (alpha < 0) {
? ? ? ? alpha = 0;
? ? ? ? } else if (alpha > 10) {
? ? ? ? alpha = 10;
? ? ? ? }
? ? ? ? int c = bufferedImage.getRGB(3, 3);
? ? ? ? // 循環每一個像素點,改變像素點的Alpha值
? ? ? ? for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
? ? ? ? for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
? ? ? ? int rgb = bufferedImage.getRGB(j2, j1);
? ? ? ? if(c==rgb){
? ? ? ? rgb = rgb & 0x00ffffff;
? ? ? ? }else{
? ? ? ? rgb = ((alpha * 255 / 10) << 24) | (rgb & 0x00ffffff);??
? ? ? ? }
? ? ? ? bufferedImage.setRGB(j2, j1, rgb);??
? ? ? ? }??
? ? ? ? }??
? ? ? ? g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());??
??
? ? } catch (Exception e) {
? ? e.printStackTrace();
? ? }finally {
return bufferedImage;
}
? ? }??
? ? public static String rootUrl = System.getProperty("user.dir")+File.separator;
? ? public static String FILE_DIR="C:\\Users\\wzk\\Desktop\\";?
? ? public static void main(String[] args) throws IOException{??
? ? ? ? File testDataDir = new File(FILE_DIR+"1.png");//去噪??
? ? ? ? BufferedImage textImage =ImageIO.read(new FileInputStream(testDataDir));
//? ? ? ? cleanImage(textImage);??
? ? ? ? BufferedImage gray = gray(textImage);//灰度化
? ? ? ? ImageIO.write(gray, "png", new File(FILE_DIR+"2.png"));
? ? }??
}

總結

以上是生活随笔為你收集整理的Java图片透明化操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。