java-I/O-bmp-反色,单原色输出
Posted CaoPengCheng&
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java-I/O-bmp-反色,单原色输出相关的知识,希望对你有一定的参考价值。
java-I/O-bmp-反色,单原色输出
BMP
BMP(Bitmap-File)图形文件是Windows采用的图形文件格式,在Windows环境下运行的所有图象处理软件都支持BMP图象文件格式。Windows系统内部各图像绘制操作都是以BMP为基础的。windows 3.0以前的BMP图文件格式与显示设备有关,因此把这种BMP图象文件格式称为设备相关位图DDB(device-dependent bitmap)文件格式。Windows 3.0以后的BMP图象文件与显示设备无关,因此把这种BMP图象文件格式称为设备无关位图dib(device-independent bitmap)格式,目的是为了让Windows能够在任何类型的显示设备上显示所存储的图象。BMP位图文件默认的文件扩展名是BMP或者bmp(有时它也会以.DIB或.rle作扩展名)。
格式
Bmp图片格式组成部分:bmp文件头(14 bytes) + 位图信息头(40 bytes) + 调色板(由颜色索引数决定) + 位图数据(由图像尺寸决定)
颜色
这里采用32位bmp,54 bytes后,每四个字节表示一个像素点,前三个字节表示颜色,以此是红绿蓝
package Input_And_Out;
import java.io.*;
public class Jpeg {
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream1 = null,bufferedOutputStream2 = null,bufferedOutputStream3 = null,bufferedOutputStream4 = null;
try {
bufferedInputStream=new BufferedInputStream(new FileInputStream("src\\\\Input_And_Out\\\\tiger.bmp"));
bufferedOutputStream1=new BufferedOutputStream(new FileOutputStream("src\\\\Input_And_Out\\\\tiger1.bmp"));
bufferedOutputStream2=new BufferedOutputStream(new FileOutputStream("src\\\\Input_And_Out\\\\tiger2.bmp"));
bufferedOutputStream3=new BufferedOutputStream(new FileOutputStream("src\\\\Input_And_Out\\\\tiger3.bmp"));
bufferedOutputStream4=new BufferedOutputStream(new FileOutputStream("src\\\\Input_And_Out\\\\tiger4.bmp"));
int count,cursor = 0;
while ((count = bufferedInputStream.read()) != -1) {
if (cursor > 54) {
//直接读四个,表示颜色
int [] b=new int[4];
b[0]=count;
for(int i=1;i<b.length;i++)
b[i]= bufferedInputStream.read();
for(int i=0;i<b.length;i++){
if(i==0) {
bufferedOutputStream2.write(b[i]);
bufferedOutputStream3.write(0);
bufferedOutputStream4.write(0);
bufferedOutputStream1.write(0xff - b[i]);
}else if(i==1){
bufferedOutputStream2.write(0);
bufferedOutputStream3.write(b[i]);
bufferedOutputStream4.write(0);
bufferedOutputStream1.write(0xff - b[i]);
}else if(i==2){
bufferedOutputStream2.write(0);
bufferedOutputStream3.write(0);
bufferedOutputStream4.write(b[i]);
bufferedOutputStream1.write(0xff - b[i]);
}else{
bufferedOutputStream2.write(b[i]);
bufferedOutputStream3.write(b[i]);
bufferedOutputStream4.write(b[i]);
bufferedOutputStream1.write(0xff - b[i]);
}
}
} else {
bufferedOutputStream1.write(count);
bufferedOutputStream2.write(count);
bufferedOutputStream3.write(count);
bufferedOutputStream4.write(count);
}
cursor++;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
assert bufferedInputStream != null;
bufferedInputStream.close();
assert bufferedOutputStream1 != null;
bufferedOutputStream1.close();
assert bufferedOutputStream2 != null;
bufferedOutputStream2.close();
assert bufferedOutputStream3 != null;
bufferedOutputStream3.close();
assert bufferedOutputStream4 != null;
bufferedOutputStream4.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
原图
输出图片
-
反色,二进制码取反
-
单绿色,保留第一字节颜色,其余置0
-
单红色,保留第二字节颜色,其余置0
-
单蓝色,保留第三字节颜色,其余置0
以上是关于java-I/O-bmp-反色,单原色输出的主要内容,如果未能解决你的问题,请参考以下文章