java getcolormodel_Java ColorModel.getTransparency方法代碼示例

import java.awt.image.ColorModel; //導入方法依賴的package包/類

/**

* Return true if the BufferedImage argument has non-opaque

* bits in it and therefore can not be directly rendered by

* GDI. Return false if the image is opaque. If this function

* can not tell for sure whether the image has transparent

* pixels then it assumes that it does.

*/

protected boolean hasTransparentPixels(BufferedImage bufferedImage) {

ColorModel colorModel = bufferedImage.getColorModel();

boolean hasTransparency = colorModel == null

? true

: colorModel.getTransparency() != ColorModel.OPAQUE;

/*

* For the default INT ARGB check the image to see if any pixels are

* really transparent. If there are no transparent pixels then the

* transparency of the color model can be ignored.

* We assume that IndexColorModel images have already been

* checked for transparency and will be OPAQUE unless they actually

* have transparent pixels present.

*/

if (hasTransparency && bufferedImage != null) {

if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||

bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {

DataBuffer db = bufferedImage.getRaster().getDataBuffer();

SampleModel sm = bufferedImage.getRaster().getSampleModel();

if (db instanceof DataBufferInt &&

sm instanceof SinglePixelPackedSampleModel) {

SinglePixelPackedSampleModel psm =

(SinglePixelPackedSampleModel)sm;

// Stealing the data array for reading only...

int[] int_data =

SunWritableRaster.stealData((DataBufferInt) db, 0);

int x = bufferedImage.getMinX();

int y = bufferedImage.getMinY();

int w = bufferedImage.getWidth();

int h = bufferedImage.getHeight();

int stride = psm.getScanlineStride();

boolean hastranspixel = false;

for (int j = y; j < y+h; j++) {

int yoff = j * stride;

for (int i = x; i < x+w; i++) {

if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {

hastranspixel = true;

break;

}

}

if (hastranspixel) {

break;

}

}

if (hastranspixel == false) {

hasTransparency = false;

}

}

}

}

return hasTransparency;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值