java中如何使用BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR
java中BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR
一般來說Java ImageIO處理讀取圖像時(shí),一般是RGB或ARGB格式,但是有的時(shí)候,我們需要圖像是BGR格式,
比如通過JNI將圖像矩陣傳遞給動(dòng)態(tài)庫,動(dòng)態(tài)庫里用OpenCV來處理矩陣,而用OpenCV處理圖像時(shí)默認(rèn)通道順序是BGR,這時(shí)就需要一個(gè)到BGR轉(zhuǎn)換。
翻了好Java API好久,還真沒發(fā)現(xiàn)有直接將RGB轉(zhuǎn)BGR的方法,于是只好自己寫一個(gè),以下是代碼片段,用于實(shí)現(xiàn)判斷BufferedImage圖像類型及通道順序,并將BufferedImage轉(zhuǎn)為RGB或BGR
實(shí)例代碼:
/** * @param image * @param bandOffset 用于判斷通道順序 * @return */ private static boolean equalBandOffsetWith3Byte(BufferedImage image,int[] bandOffset){ if(image.getType()==BufferedImage.TYPE_3BYTE_BGR){ if(image.getData().getSampleModel() instanceof ComponentSampleModel){ ComponentSampleModel sampleModel = (ComponentSampleModel)image.getData().getSampleModel(); if(Arrays.equals(sampleModel.getBandOffsets(), bandOffset)){ return true; } } } return false; } /** * 判斷圖像是否為BGR格式 * @return */ public static boolean isBGR3Byte(BufferedImage image){ return equalBandOffsetWith3Byte(image,new int[]{0, 1, 2}); } /** * 判斷圖像是否為RGB格式 * @return */ public static boolean isRGB3Byte(BufferedImage image){ return equalBandOffsetWith3Byte(image,new int[]{2, 1, 0}); } /** * 對(duì)圖像解碼返回RGB格式矩陣數(shù)據(jù) * @param image * @return */ public static byte[] getMatrixRGB(BufferedImage image) { if(null==image) throw new NullPointerException(); byte[] matrixRGB; if(isRGB3Byte(image)){ matrixRGB= (byte[]) image.getData().getDataElements(0, 0, image.getWidth(), image.getHeight(), null); }else{ // 轉(zhuǎn)RGB格式 BufferedImage rgbImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR); new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB), null).filter(image, rgbImage); matrixRGB= (byte[]) rgbImage.getData().getDataElements(0, 0, image.getWidth(), image.getHeight(), null); } return matrixRGB; } /** * 對(duì)圖像解碼返回BGR格式矩陣數(shù)據(jù) * @param image * @return */ public static byte[] getMatrixBGR(BufferedImage image){ if(null==image) throw new NullPointerException(); byte[] matrixBGR; if(isBGR3Byte(image)){ matrixBGR= (byte[]) image.getData().getDataElements(0, 0, image.getWidth(), image.getHeight(), null); }else{ // ARGB格式圖像數(shù)據(jù) int intrgb[]=image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth()); matrixBGR=new byte[image.getWidth() * image.getHeight()*3]; // ARGB轉(zhuǎn)BGR格式 for(int i=0,j=0;i<intrgb.length;++i,j+=3){ matrixBGR[j]=(byte) (intrgb[i]&0xff); matrixBGR[j+1]=(byte) ((intrgb[i]>>8)&0xff); matrixBGR[j+2]=(byte) ((intrgb[i]>>16)&0xff); } } return matrixBGR; }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java實(shí)現(xiàn)調(diào)用接口API并返回?cái)?shù)據(jù)
這篇文章主要介紹了Java實(shí)現(xiàn)調(diào)用接口API并返回?cái)?shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05重試框架Guava-Retry和spring-Retry的使用示例
spring-retry 和 guava-retry 工具都是線程安全的重試,能夠支持并發(fā)業(yè)務(wù)場(chǎng)景的重試邏輯正確性,本文主要介紹了重試框架Guava-Retry和spring-Retry的使用示例,感興趣的可以一下2023-09-09mybatis-plus配置日志兩種實(shí)現(xiàn)方式
這篇文章主要給大家介紹了關(guān)于mybatis-plus配置日志兩種實(shí)現(xiàn)方式的相關(guān)資料,Mybatis-plus集成了日志框架,可以將程序運(yùn)行時(shí)產(chǎn)生的日志進(jìn)行記錄,方便開發(fā)人員進(jìn)行問題排查,需要的朋友可以參考下2023-09-09java poi導(dǎo)出excel時(shí)如何設(shè)置手動(dòng)換行
這篇文章主要介紹了java poi導(dǎo)出excel時(shí)如何設(shè)置手動(dòng)換行,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06SpringDataJpa如何使用union多表分頁條件查詢
這篇文章主要介紹了SpringDataJpa如何使用union多表分頁條件查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(25)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07