3232
3333public class BufferedImageUtil {
3434
35+ public static boolean halfTransparent (BufferedImage image ){
36+ Color color = new Color ();
37+ float [] buffer = null ;
38+ for (int x = 0 ; x < image .getWidth (); x ++){
39+ for (int y = 0 ; y < image .getHeight (); y ++){
40+ buffer = readPixel (image , x , y , color , buffer );
41+ if (color .a > 0f && color .a < 1f ) return true ;
42+ }
43+ }
44+ return false ;
45+ }
46+
3547 public static Color averageColor (BufferedImage image ) {
3648 Color average = new Color ();
3749 Color color = new Color ();
@@ -58,7 +70,7 @@ private static float[] readPixel(BufferedImage image, int x, int y, @Nullable Co
5870 if (target == null ) target = new Color ();
5971
6072 // workaround for java bug: 5051418
61- if (image .getType () == BufferedImage .TYPE_BYTE_GRAY ) {
73+ if (image .getType () == BufferedImage .TYPE_BYTE_GRAY || image . getType () == BufferedImage . TYPE_CUSTOM ) {
6274 buffer = readPixelDirect (image , x , y , target , buffer );
6375 } else {
6476 readPixelDefault (image , x , y , target );
@@ -74,10 +86,10 @@ private static void readPixelDefault(BufferedImage image, int x, int y, Color ta
7486 private static float [] readPixelDirect (RenderedImage image , int x , int y , Color target , float @ Nullable [] buffer ) {
7587 buffer = image .getData ().getPixel (x , y , buffer );
7688
77- float a = buffer .length >= 4 ? buffer [3 ] / 255f : 1f ;
7889 float r = buffer [0 ] / 255f ;
7990 float g = buffer .length >= 3 ? buffer [1 ] / 255f : r ;
8091 float b = buffer .length >= 3 ? buffer [2 ] / 255f : r ;
92+ float a = buffer .length >= 4 ? buffer [3 ] / 255f : buffer .length == 2 ? buffer [1 ] / 255f : 1f ;
8193
8294 target .set (r , g , b , a , image .getColorModel ().isAlphaPremultiplied ());
8395
0 commit comments