java byte数组的RBG转int的RBG,从而用BufferedImage表示图像

来源:互联网 发布:淘宝贷款10万一年利息 编辑:程序博客网 时间:2024/06/04 17:51
刚接触opencv,想显示个图片都不会,所以想写一个opencv的图像类转BufferedImage的方法
<span style="font-family: Arial, Helvetica, sans-serif;">BufferedImage mat2BufferedImage(Mat mat){BufferedImage bi=new BufferedImage(mat.width(),mat.height(),BufferedImage.TYPE_INT_RGB );//这里t的type不太懂for(int i=0;i<mat.rows();i++){//mat.height()for(int j=0;j<mat.cols();j++){byte[] b=new byte[3];mat.get(i, j, b);//把rgb值放入bint rgb=((int)b[0])&0xff;//&0xff是为了不让结果变为负数//不知道为什么获取的b[0]是蓝色的rgb|=(((int)b[1])&0xff)<<8;//每8位放一个值rgb|=(((int)b[2])&0xff)<<16;rgb|=0xff000000;//从BufferedImage.getRBG()看出,这个类表示的RBG左边8位均为1bi.setRGB(j, i, rgb);}}return bi;}</span>


0 0
原创粉丝点击