A. java 怎么实现读取8位的bmp图片文件
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
public class Test{
public static void main(String args[]) {
int[] rgb = new int[3];
File file = new File("a.bmp");
BufferedImage bi=null;
try{
bi = ImageIO.read(file);
}catch(Exception e){
e.printStackTrace();
}
int width=bi.getWidth();
int height=bi.getHeight();
int minx=bi.getMinX();
int miny=bi.getMinY();
System.out.println("width="+width+",height="+height+".");
System.out.println("minx="+minx+",miniy="+miny+".");
for(int i=minx;i<width;i++){
for(int j=miny;j<height;j++){
//System.out.print(bi.getRGB(jw, ih));
int pixel=bi.getRGB(i, j);
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
System.out.println("i="+i+",j="+j+":("+rgb[0]+","+rgb[1]+","+rgb[2]+")");
}
}
}
}
B. java如何获得bmp图片的dpi值,记得有个水平分辨率和垂直分辨率
1、读取一个bmp文件,把bmp的所有像素用rgbArray存储起来。
2、然后取其中一个像素点(x0,y0),把它构造成一个Color对象。
3、构造一个类型一样的BufferedImage imgOut,把像素矩阵rgbArray写到BufferedImage。
4、把imgOut写入文件
这个Color对象有getRed,getBlue,getBlack方法,可以分别获取这个像素在三个颜色分量上的灰度值。
C. 如何用java实现1bit bmp位图读取
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class TestWin extends JFrame implements ActionListener {
private JLabel imgLabel = new JLabel();
public TestWin() {
try {
Image img=ImageIO.read(new File("1bit.bmp"));
imgLabel.setIcon(new ImageIcon(img));
} catch (IOException e) {
e.printStackTrace();
}
add(imgLabel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestWin().setVisible(true);
}
});
}
}
D. Java处理bmp图像,怎样操作BMP位图的数据
bmp图像文件数据分为三个部分:
1、前14个字节为文件信息头,在这部分信息中包含了位图信息标志、该bmp图像的大小和图像实际数据的相对偏移量这三部分有用的信息。
位图标志一定为“0x4D42”,否则,该文件不是bmp图像。
在VC++中,这14个字节对应一个数据类型,类型名为“BITMAPFILEHEADER”,它的定义为:
typedef struct tagBITMAPFILEHEADER {
WORD bfType; //位图信息标志
DWORD bfSize; //图像的大小
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits; //图像实际数据的相对偏移量
} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;
可以设一个该类型的变量:BITMAPFILEHEADER bmfh,将bmp图像文件的前14字节数据读入这个变量中,然后通过判断bmfh.bfType == 0x4D42,确定是不是为bmp图像。
2、接下来40个字节为位图信息头,其中存储了该bmp图像的有关信息。这些信息包括:图像宽度(像素)、图像高度(像素)、图像长度(字节,仅仅是图像实际数据的长度,不包括各个信息头)、水平分辨率、垂直分辨率、每个像素的存储位数等信息。
其中,通过“每个像素的存储位数”这个信息可以知道图像的颜色:
如果“每个像素的存储位数”的值只有四种:为1,说明图像只有两种颜色(黑、白);为4,说明图像有16种颜色;为8,说明图像有256种颜色;为24,说明该图像为真彩色图像,颜色数为2^24。这四种取值对应四种bmp图像,也就是说,bmp图像只有这四种。
在这四种bmp图像种,前三种都需要在图像文件中包含调色板数据,分别存储三种图像的2、16、256种颜色。而最后一种bmp格式的图像不需要调色板,因为这种图像的“每个像素的存储位数”值为24,也就是说,存储一个像素值需要24位,正好可以存储一个像素的颜色(红、绿、蓝各8位)。
在VC++中,这40个字节的位图信息头也有一个数据类型,类型名为“BITMAPINFOHEADER”,它的定义为:
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth; //图像宽度(像素)
LONG biHeight; //图像高度(像素)
WORD biPlanes;
WORD biBitCount; //每个像素的存储位数
DWORD biCompression;
DWORD biSizeImage; //图像长度(字节,仅仅是图像实际数据的长度,不包括各个信息头)
LONG biXPelsPerMeter; //水平分辨率
LONG biYPelsPerMeter; //垂直分辨率
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
3、接下来若干个字节为调色板,只有前三种bmp图像有,第四种真彩色bmp图像没有这部分数据。
调色板是一个数组,每个数组元素有四字节,只有三个字节有用,另外一个没有。有用的三个字节存储一种颜色(红绿蓝各占一字节),这四个字节在VC++中定义为:
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
定义一个这种类型的数组即为调色板。数组的长度可由BITMAPINFOHEADER中的biBitCount推算出来。
4、上述三部分信息之后,即是实际的像素数据。一个像素的存储位数为1、4、8或16,正如前面所述。
如果是1位,对应的bmp图像应该有一个长度为2的调色板。这一位的值只能是0或1,用来指明该像素的颜色在调色板中的地址。
如果是4位,对应的bmp图像应该有一个长度为16的调色板。这4位的值有16种,同样指示该像素的颜色在调色板中的地址。
如果是8位,对应的bmp图像应该有一个长度为256的调色板。这8位的值有256种,同样指示该像素的颜色在调色板中的地址。
如果是24位,对应的bmp图像没有调色板,该像素的颜色由这24位数据直接表示。
bmp图像的数据就这几个部分。
任何一个bmp图像的像素都是由红绿蓝三种颜色组成(带调色板也好,不带调色板也好)。如果一个像素的红绿蓝三种色的值相等,那么该像素就是灰色的。灰度图是这样一种有严格规定的bmp图像:它是上述四种bmp图像的第三种,并且它的调色板的每个数组元素的红绿蓝三值都相同,所以灰度图的灰度种数是256。
若要保存图像,需要按顺序保存文件信息头、位图信息头、调色板(如果有)和图像的实际数据。程序可以这样写:
bool Write(CString FileName)
{
CFile file;
BITMAPFILEHEADER bmfh;
if(! (bmi && pBits))
{
AfxMessageBox("Data is not valid!");
return FALSE;
}
//创建文件
if(!file.Open(FileName,CFile::modeCreate | CFile::modeWrite))
{
AfxMessageBox("File creating fails!");
return FALSE;
}
//填写文件信息头
bmfh.bfType = 0x4d42;
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
int nInfoSize = sizeof(BITMAPINFOHEADER) + GetPaletteSize() * sizeof(RGBQUAD);
bmfh.bfOffBits = sizeof(bmfh) + nInfoSize;
bmfh.bfSize = bmfh.bfOffBits + bmi->bmiHeader.biSizeImage;
//写文件
file.Write( (LPVOID)&bmfh, sizeof(bmfh));
file.Write( (LPVOID)bmi, nInfoSize);
file.Write( (LPVOID)pBits, bmi->bmiHeader.biSizeImage);
return TRUE;
}
E. java为何不能用bmp图片
使用ToolKit类的createImage不能显示bmp格式的图片;
改用ImageIO.read(this.getClass().getClassLoader().getResource(file));来读取任意格式图片。
/**
* 读取任意图片文件
*
* @param file
* @return
*/
public Image loadAnyImage(String file)
{
Image image = null; // 构造一个目标图
InputStream fs = null;
try
{
fs = new BufferedInputStream(getClass().getClassLoader()
.getResourceAsStream(file));
int bflen = 14;
byte[] bf = new byte[bflen];
fs.read(bf, 0, bflen); // 读取14字节BMP文件头
int bilen = 40;
byte[] bi = new byte[bilen];
fs.read(bi, 0, bilen); // 读取40字节BMP信息头
// 获取一些重要数据
int nwidth = (((int) bi[7] & 0xff) << 24) // 源图宽度
| (((int) bi[6] & 0xff) << 16)
| (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xff;
int nheight = (((int) bi[11] & 0xff) << 24) // 源图高度
| (((int) bi[10] & 0xff) << 16)
| (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xff;
// 位数
int nbitcount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff;
// 源图大小
int nsizeimage = (((int) bi[23] & 0xff) << 24)
| (((int) bi[22] & 0xff) << 16)
| (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff;
// 对24位BMP进行解析
if (nbitcount == 24)
{
int npad = (nsizeimage / nheight) - nwidth * 3;
int[] ndata = new int[nheight * nwidth];
byte[] brgb = new byte[(nwidth + npad) * 3 * nheight];
fs.read(brgb, 0, (nwidth + npad) * 3 * nheight);
int nindex = 0;
for (int j = 0; j < nheight; j++)
{
for (int i = 0; i < nwidth; i++)
{
ndata[nwidth * (nheight - j - 1) + i] = (255 & 0xff) << 24
| (((int) brgb[nindex + 2] & 0xff) << 16)
| (((int) brgb[nindex + 1] & 0xff) << 8)
| (int) brgb[nindex] & 0xff;
nindex += 3;
}
nindex += npad;
}
Toolkit kit = Toolkit.getDefaultToolkit();
image = kit.createImage(new MemoryImageSource(nwidth, nheight,
ndata, 0, nwidth));
}
// 8位BMP或其他图片格式
else
{
image = javax.imageio.ImageIO.read(getClass().getClassLoader()
.getResource(file));
}
}
catch (Exception e)
{
}
finally
{
try
{
if (null != fs)
{
fs.close();
fs = null;
}
}
catch (IOException e)
{
}
}
return image;
}
F. 怎样用c语言c++编码打开读取bmp。两者的的区别。可否用JAVA编写bmp。为什么
1C++读取bmp文件实例
2java读取bmp文件
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define WIDTHBYTES(bits) (((bits)+31)/32*4)
typedef unsigned char BYTE;//字符型
typedef unsigned short WORD;//短整型
typedef unsigned long DWORD;//长整形
typedef long LONG;
//位图文件头信息结构定义
//其中不包含文件类型信息(由于结构体的内存结构决定,要是加了的话将不能正确读取文件信息)
typedef struct tagBITMAPFILEHEADER {
//WORD bfType;
DWORD bfSize; //文件大小
WORD bfReserved1; //保留字,不考虑
WORD bfReserved2; //保留字,同上
DWORD bfOffBits; //实际位图数据的偏移字节数,即前三个部分长度之和
} BITMAPFILEHEADER;
//信息头BITMAPINFOHEADER,也是一个结构,其定义如下:
typedef struct tagBITMAPINFOHEADER{
//public:
DWORD biSize; //指定此结构体的长度,为40
LONG biWidth; //位图宽
LONG biHeight; //位图高
WORD biPlanes; //平面数,为1
WORD biBitCount; //采用颜色位数,可以是1,2,4,8,16,24,新的可以是32
DWORD biCompression; //压缩方式,可以是0,1,2,其中0表示不压缩
DWORD biSizeImage; //实际位图数据占用的字节数
LONG biXPelsPerMeter; //X方向分辨率
LONG biYPelsPerMeter; //Y方向分辨率
DWORD biClrUsed; //使用的颜色数,如果为0,则表示默认值(2^颜色位数)
DWORD biClrImportant; //重要颜色数,如果为0,则表示所有颜色都是重要的
} BITMAPINFOHEADER;
//调色板Palette,当然,这里是对那些需要调色板的位图文件而言的。24位和32位是不需要调色板的。
//(似乎是调色板结构体个数等于使用的颜色数。)
typedef struct tagRGBQUAD {
//public:
BYTE rgbBlue; //该颜色的蓝色分量
BYTE rgbGreen; //该颜色的绿色分量
BYTE rgbRed; //该颜色的红色分量
BYTE rgbReserved; //保留值
} RGBQUAD;
void showRgbQuan(tagRGBQUAD* pRGB)
{
printf("(%-3d,%-3d,%-3d) ",pRGB->rgbRed,pRGB->rgbGreen,pRGB->rgbBlue);
}
int main(){
char ch,pd;
int b,c,m,d,w,e,f;
int x,y;
unsigned char *q;
int i,j;
unsigned char a[2];
char strFile[50];
BITMAPFILEHEADER bitHead;
BITMAPINFOHEADER bitInfoHead;
tagRGBQUAD *pRgb ;
FILE *fp;
FILE *p;
q=(unsigned char*)malloc(1);
printf("mkx**用C语言编程来读取BMP文件某一像素点的数据** 请输入一个bmp文件: ");
scanf("%s",strFile);
fp=fopen(strFile,"rb");
if(fp!=NULL)
{
printf("file open success! ");
WORD fileType;
fread(&fileType,1,sizeof(WORD),fp);
if(fileType != 0x4d42)
{
printf("file is not .bmp file!");
system("pause");
return 0;
}
}
else
{
printf("file open fail! ");
system("pause");
return 0;
}
//读文件头信息,并打印文件头信息各项的值
fread(&bitHead,1,sizeof(tagBITMAPFILEHEADER),fp);
printf("bmp文件头信息 文件大小:%d 保留字:%d 保留字:%d 实际位图数据的偏移字节数:%d ",
bitHead.bfSize,bitHead.bfReserved1,bitHead.bfReserved2,bitHead.bfOffBits);
//读文件信息头,并打印文件信息头各项的值
fread(&bitInfoHead,1,sizeof(tagBITMAPINFOHEADER),fp);
printf("bmp文件信息头 结构体的长度:%d 位图宽:%d 位图高:%d biPlanes平面数:%d biBitCount采用颜色位数:%d 压缩方式:%d biSizeImage实际位图数据占用的字节数:%d X方向分辨率:%d Y方向分辨率:%d 使用的颜色数:%d 重要颜色数:%d ",
bitInfoHead.biSize,bitInfoHead.biWidth,bitInfoHead.biHeight,bitInfoHead.biPlanes,bitInfoHead.biBitCount,bitInfoHead.biCompression,bitInfoHead.biSizeImage,bitInfoHead.biXPelsPerMeter,bitInfoHead.biYPelsPerMeter,bitInfoHead.biClrUsed,bitInfoHead.biClrImportant);
if(bitInfoHead.biBitCount < 24){
printf("该文件有调色板,即该位图为非真彩色 ");
m=1;
if(bitInfoHead.biBitCount =8){
long nPlantNum = long(pow(2,double(bitInfoHead.biBitCount))); // Mix color Plant Number;
pRgb=(tagRGBQUAD *)malloc(nPlantNum*sizeof(tagRGBQUAD));
memset(pRgb,0,nPlantNum*sizeof(tagRGBQUAD));
int num = fread(pRgb,4,nPlantNum,fp);
printf("Color Plate Number: %d ",nPlantNum);
printf("颜色板信息: ");
for (int i =0; i<nPlantNum;i++)
{
if (i%5==0)
{
printf(" ");
}
showRgbQuan(&pRgb[i]);
}
printf(" ");
}
}
else{
printf("该位图为24位真彩色 ");
m=3;
}
fclose(fp);
while(ch!='N'){
p=fopen(strFile,"rb");
ch=NULL;
f=bitInfoHead.biHeight;
e=bitInfoHead.biWidth;
b=bitHead.bfOffBits;
printf("**输入指定像素点的数据**(行:1~%d 列1~%d) ",f,e);
printf("请输入第i行:");
scanf("%d",&x);
printf("请输入第j列:");
scanf("%d",&y);
c=b+(x-1)*e*m+m*(y-1);
fseek(p,c,0);
//printf("%d ",m);
if(m<2){
fread(&a[0],1,1,p);
printf("该点像素点的数据(十六进制)为:%x ",a[0]);
printf("用十进制表示:%d ",a[0]);
d=a[0];
printf("它对应的rgb值为:");
showRgbQuan(&pRgb[d]);
printf(" ");
}
else {
for(i=0;i<3;i++){
fread(&a[i],1,1,p);
// printf("%x ",a[i]);
}
printf("十六进制表示: 蓝:%x 绿:%x 红:%x ",a[0],a[1],a[2]);
printf("十进制表示: 蓝:%d 绿:%d 红:%d ",a[0],a[1],a[2]);
}
rewind(p);
//w=ftell(p);
//printf("%d",w);
fclose(p);
printf("**继续请输入任意字符,如需退出请输入N ");
getchar();
scanf("%c",&ch);
}
printf("感谢您使用~ ");
system("pause");
return 0;
}
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
//
//really just a collection of methods to read a BMP file
//
public class BMPLoader
{
// build an int from a byte array - convert little to big endian
public static int constructInt(byte[] in, int offset) {
int ret = ((int) in[offset + 3] & 0xff);
ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
return (ret);
}
// build an int from a byte array - convert little to big endian
// set high order bytes to 0xfff
public static int constructInt3(byte[] in, int offset) {
int ret = 0xff;
ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
return (ret);
}
// build an int from a byte array - convert little to big endian
public static long constructLong(byte[] in, int offset) {
long ret = ((long) in[offset + 7] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);
return (ret);
}
// build an double from a byte array - convert little to big endian
public static double constructDouble(byte[] in, int offset) {
long ret = constructLong(in, offset);
return (Double.longBitsToDouble(ret));
}
// build an short from a byte array - convert little to big endian
public static short constructShort(byte[] in, int offset) {
short ret = (short) ((short) in[offset + 1] & 0xff);
ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));
return (ret);
}
// internal class representing a bitmap header structure
// with code to read it from a file
static class BitmapHeader {
public int nsize;
public int nbisize;
public int nwidth;
public int nheight;
public int nplanes;
public int nbitcount;
public int ncompression;
public int nsizeimage;
public int nxpm;
public int nypm;
public int nclrused;
public int nclrimp;
// read in the bitmap header
public void read(FileInputStream fs) throws IOException
{
final int bflen = 14; // 14 byte BITMAPFILEHEADER
byte bf[] = new byte[bflen];
fs.read(bf, 0, bflen);
final int bilen = 40; // 40-byte BITMAPINFOHEADER
byte bi[] = new byte[bilen];
fs.read(bi, 0, bilen);
// Interperet data.
nsize = constructInt(bf, 2);
// System.out.println("File type is :"+(char)bf[0]+(char)bf[1]);
// System.out.println("Size of file is :"+nsize);
nbisize = constructInt(bi, 2);
// System.out.println("Size of bitmapinfoheader is :"+nbisize);
nwidth = constructInt(bi, 4);
// System.out.println("Width is :"+nwidth);
nheight = constructInt(bi, 8);
// System.out.println("Height is :"+nheight);
nplanes = constructShort(bi, 12); //(((int)bi[13]&0xff)<<8) |
// (int)bi[12]&0xff;
// System.out.println("Planes is :"+nplanes);
nbitcount = constructShort(bi, 14); //(((int)bi[15]&0xff)<<8) |
// (int)bi[14]&0xff;
// System.out.println("BitCount is :"+nbitcount);
// Look for non-zero values to indicate compression
ncompression = constructInt(bi, 16);
// System.out.println("Compression is :"+ncompression);
nsizeimage = constructInt(bi, 20);
// System.out.println("SizeImage is :"+nsizeimage);
nxpm = constructInt(bi, 24);
// System.out.println("X-Pixels per meter is :"+nxpm);
nypm = constructInt(bi, 28);
// System.out.println("Y-Pixels per meter is :"+nypm);
nclrused = constructInt(bi, 32);
// System.out.println("Colors used are :"+nclrused);
nclrimp = constructInt(bi, 36);
// System.out.println("Colors important are :"+nclrimp);
}
}
public static Image read(FileInputStream fs)
{
try {
BitmapHeader bh = new BitmapHeader();
bh.read(fs);
if (bh.nbitcount == 24)
return (readMap24(fs, bh));
if (bh.nbitcount == 32)
return (readMap32(fs, bh));
if (bh.nbitcount == 8)
return (readMap8(fs, bh));
fs.close();
} catch (IOException e) {
// System.out.println("Caught exception in loadbitmap!");
}
return (null);
}
/**
*
* readMap24 internal routine to read the bytes in a 24 bit bitmap
*
*
*
* Arguments:
*
* fs - file stream
*
* bh - header struct
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
protected static Image readMap32(FileInputStream fs, BitmapHeader bh)
throws IOException
{
Image image;
// No Palatte data for 24-bit format but scan lines are
// padded out to even 4-byte boundaries.
int xwidth = bh.nsizeimage / bh.nheight;
int ndata[] = new int[bh.nheight * bh.nwidth];
byte brgb[] = new byte[bh.nwidth * 4 * bh.nheight];
fs.read(brgb, 0, bh.nwidth * 4 * bh.nheight);
int nindex = 0;
for (int j = 0; j < bh.nheight; j++)
{
for (int i = 0; i < bh.nwidth; i++)
{
ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(
brgb, nindex);
nindex += 4;
}
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.nwidth, bh.nheight,
ndata, 0, bh.nwidth));
fs.close();
return (image);
}
/**
*
* readMap24 internal routine to read the bytes in a 24 bit bitmap
*
*
*
* Arguments:
*
* fs - file stream
*
* bh - header struct
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
protected static Image readMap24(FileInputStream fs, BitmapHeader bh)
throws IOException
{
Image image;
// No Palatte data for 24-bit format but scan lines are
// padded out to even 4-byte boundaries.
int npad = (bh.nsizeimage / bh.nheight) - bh.nwidth * 3;
int ndata[] = new int[bh.nheight * bh.nwidth];
byte brgb[] = new byte[(bh.nwidth + npad) * 3 * bh.nheight];
fs.read(brgb, 0, (bh.nwidth + npad) * 3 * bh.nheight);
int nindex = 0;
for (int j = 0; j < bh.nheight; j++)
{
for (int i = 0; i < bh.nwidth; i++)
{
ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(
brgb, nindex);
nindex += 3;
}
nindex += npad;
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.nwidth, bh.nheight,
ndata, 0, bh.nwidth));
fs.close();
return (image);
}
/**
*
* readMap8 internal routine to read the bytes in a 8 bit bitmap
*
*
*
* Arguments:
*
* fs - file stream
*
* bh - header struct
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
protected static Image readMap8(FileInputStream fs, BitmapHeader bh)
throws IOException
{
Image image;
// Have to determine the number of colors, the clrsused
// parameter is dominant if it is greater than zero. If
// zero, calculate colors based on bitsperpixel.
int nNumColors = 0;
if (bh.nclrused > 0)
{
nNumColors = bh.nclrused;
}
else
{
nNumColors = (1 & 0xff) << bh.nbitcount;
}
// System.out.println("The number of Colors is"+nNumColors);
// Some bitmaps do not have the sizeimage field calculated
// Ferret out these cases and fix 'em.
if (bh.nsizeimage == 0)
{
bh.nsizeimage = ((((bh.nwidth * bh.nbitcount) + 31) & ~31) >> 3);
bh.nsizeimage *= bh.nheight;
// System.out.println("nsizeimage (backup) is"+nsizeimage);
}
// Read the palatte colors.
int npalette[] = new int[nNumColors];
byte bpalette[] = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++)
{
npalette[n] = constructInt3(bpalette, nindex8);
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (bh.nsizeimage / bh.nheight) - bh.nwidth;
// System.out.println("nPad is:"+npad8);
int ndata8[] = new int[bh.nwidth * bh.nheight];
byte bdata[] = new byte[(bh.nwidth + npad8) * bh.nheight];
fs.read(bdata, 0, (bh.nwidth + npad8) * bh.nheight);
nindex8 = 0;
for (int j8 = 0; j8 < bh.nheight; j8++)
{
for (int i8 = 0; i8 < bh.nwidth; i8++)
{
ndata8[bh.nwidth * (bh.nheight - j8 - 1) + i8] =
npalette[((int) bdata[nindex8] & 0xff)];
nindex8++;
}
nindex8 += npad8;
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.nwidth, bh.nheight,
ndata8, 0, bh.nwidth));
return (image);
}
/**
*
* load method - see read for details
*
*
*
* Arguments:
*
* sdir and sfile are the result of the FileDialog()
*
* getDirectory() and getFile() methods.
*
*
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
public static Image load(String sdir, String sfile) {
return (load(sdir + sfile));
}
/**
*
* load method - see read for details
*
*
*
* Arguments:
*
* sdir - full path name
*
*
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
public static Image load(String sdir)
{
try
{
FileInputStream fs = new FileInputStream(sdir);
return (read(fs));
}
catch (IOException ex) {
return (null);
}
}
public static void main(String[] args) throws IOException
{
if (args.length == 0) {
System.out.println("Usage >java BMPLoader ImageFile.bmp");
System.exit(0);
}
FileInputStream in = new FileInputStream(args[0]);
Image TheImage = read(in);
JFrame TheFrame = new JFrame(args[0]);
JLabel TheLabel = new JLabel(new ImageIcon(TheImage));
TheFrame.getContentPane().add(new JScrollPane(TheLabel));
TheFrame.setSize(300, 300);
TheFrame.setVisible(true);
}
//end class BMPLoader
}
G. 菜鸟求JAVA程序一个能读取bmp图像并能用gui的窗口显示出图像
用JLabel
JLabel l = new JLabel();
l.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/img.bmp")));
H. 怎么用JAVA打开一个已经存在的bmp文件
BMP(Bitmap-File)图形文件是Windows采用的图形文件格式,在Windows环境下运行的所有图象处理软件都支持BMP图象文件格式。找找你系统上能打开这个图片的软件。
I. Java如何读取BMP的每个像素点,输出到一个二维数组
楼上的基本正确,
问题一:
int[] rgb = new int[3];最好用二维数组
int[] rgb = new int[3][width*height]
问题二:
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
会把数组内的值覆盖,获得就是最后像素点的RGB值;
我写了一个希望可以帮助到你
package imageReadAndWrite;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* JPG File reader/writer. Uses native com.sun libraries (which may deprecate at
* any time)
*
*
* @author PhoenixZJG
* @version 1.0
*/
public class JPGFile implements xxxFile {
private int[] ints = null;
private byte bytes[] = null; // bytes which make up binary PPM image
private double doubles[] = null;
private int[][] imageRGB = null;
private String filename = null; // filename for PPM image
private int height = 0;
private int width = 0;
/**
* Read the PPM File.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public JPGFile(String filename) throws FileNotFoundException, IOException {
this.filename = filename;
readImage();
}
/**
* Get the height of the PPM image.
*
* @return the height of the image.
*/
public int getHeight() {
return height;
}
/**
* Get the width of the PPM image.
*
* @return the width of the image.
*/
public int getWidth() {
return width;
}
/**
* Get the data as byte array. Data is of any type that has been read from
* the file (usually 8bit RGB)
*
* @return The data of the image.
*/
public byte[] getBytes() {
return bytes;
}
/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public double[] getDouble() {
return doubles;
}
/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[] getInt() {
return ints;
}
/**
* Get the data as integer array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[][] getImageRGB() {
return imageRGB;
}
/**
* Write to <code>fn</code> file the <code>data</code> using the
* <code>width, height</code> variables. Data is assumed to be 8bit RGB.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public static void writeImage(String fn, int[] data, int width, int height)
throws FileNotFoundException, IOException {
FileOutputStream fOut = new FileOutputStream(fn);
JPEGImageEncoder jpeg_encode = JPEGCodec.createJPEGEncoder(fOut);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, data, 0, width);
jpeg_encode.encode(image);
fOut.close();
}
/**
* Read the image from the specified file.
*
* @throws FileNotFoundException
* pretty obvious
* @throws IOException
* filesystem related problems
*/
private void readImage() throws FileNotFoundException, IOException {
FileInputStream fIn = new FileInputStream(filename);
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();
width = image.getWidth();
height = image.getHeight();
int[] rgbdata = new int[width * height];
image.getRGB(0, 0, width, height, rgbdata, 0, width);
ints = rgbdata;
bytes = new byte[rgbdata.length];
doubles = new double[rgbdata.length];
imageRGB = new int[3][rgbdata.length];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (rgbdata[i] & 0xFF);
doubles[i] = (double) (rgbdata[i]);
imageRGB[0][i] = (rgbdata[i] & 16711680) >> 16;
imageRGB[1][i] = (rgbdata[i] & 65280) >> 8;
imageRGB[2][i] = (rgbdata[i] & 255);
}
}
}
上述代码可以复制,粘贴使用,有方法的注视,getImageRGB() 就可以获得所有像素的RGB值,你就可以在其他方法里处理这个二维数组,得到你想要的平均值了,处理后的值,记得把值逆运算,再转换到Int[]里,就可以用,writeImage()输出图像了。
J. java 怎么得到一张bmp图片的像素数组
//我在程序中打印出了每一个坐标的RGB值,你自己整理整理,求个平均值,
//放到你的那个二维数组里。
//自己用画图工具做一个小图片,注意图片的名字和程序中一致哦~
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
public class Test{
public static void main(String args[]) {
int[] rgb = new int[3];
File file = new File("a.bmp");
BufferedImage bi=null;
try{
bi = ImageIO.read(file);
}catch(Exception e){
e.printStackTrace();
}
int width=bi.getWidth();
int height=bi.getHeight();
int minx=bi.getMinX();
int miny=bi.getMinY();
System.out.println("width="+width+",height="+height+".");
System.out.println("minx="+minx+",miniy="+miny+".");
for(int i=minx;i<width;i++){