導航:首頁 > 編程語言 > java讀取bmp

java讀取bmp

發布時間:2022-05-09 04:52:06

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 &lt;string.h&gt;
#include &lt;math.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;malloc.h&gt;
#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-&gt;rgbRed,pRGB-&gt;rgbGreen,pRGB-&gt;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(&amp;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(&amp;bitHead,1,sizeof(tagBITMAPFILEHEADER),fp);
printf("bmp文件頭信息 文件大小:%d 保留字:%d 保留字:%d 實際點陣圖數據的偏移位元組數:%d ",
bitHead.bfSize,bitHead.bfReserved1,bitHead.bfReserved2,bitHead.bfOffBits);
//讀文件信息頭,並列印文件信息頭各項的值
fread(&amp;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 &lt; 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&lt;nPlantNum;i++)
{
if (i%5==0)
{
printf(" ");
}
showRgbQuan(&amp;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",&amp;x);
printf("請輸入第j列:");
scanf("%d",&amp;y);
c=b+(x-1)*e*m+m*(y-1);
fseek(p,c,0);
//printf("%d ",m);
if(m&lt;2){
fread(&amp;a[0],1,1,p);
printf("該點像素點的數據(十六進制)為:%x ",a[0]);
printf("用十進製表示:%d ",a[0]);
d=a[0];
printf("它對應的rgb值為:");
showRgbQuan(&amp;pRgb[d]);
printf(" ");
}
else {
for(i=0;i&lt;3;i++){
fread(&amp;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",&amp;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] &amp; 0xff);
ret = (ret &lt;&lt; 8) | ((int) in[offset + 2] &amp; 0xff);
ret = (ret &lt;&lt; 8) | ((int) in[offset + 1] &amp; 0xff);
ret = (ret &lt;&lt; 8) | ((int) in[offset + 0] &amp; 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 &lt;&lt; 8) | ((int) in[offset + 2] &amp; 0xff);
ret = (ret &lt;&lt; 8) | ((int) in[offset + 1] &amp; 0xff);
ret = (ret &lt;&lt; 8) | ((int) in[offset + 0] &amp; 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] &amp; 0xff);
ret |= (ret &lt;&lt; 8) | ((long) in[offset + 6] &amp; 0xff);
ret |= (ret &lt;&lt; 8) | ((long) in[offset + 5] &amp; 0xff);
ret |= (ret &lt;&lt; 8) | ((long) in[offset + 4] &amp; 0xff);
ret |= (ret &lt;&lt; 8) | ((long) in[offset + 3] &amp; 0xff);
ret |= (ret &lt;&lt; 8) | ((long) in[offset + 2] &amp; 0xff);
ret |= (ret &lt;&lt; 8) | ((long) in[offset + 1] &amp; 0xff);
ret |= (ret &lt;&lt; 8) | ((long) in[offset + 0] &amp; 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] &amp; 0xff);
ret = (short) ((ret &lt;&lt; 8) | (short) ((short) in[offset + 0] &amp; 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]&amp;0xff)&lt;&lt;8) |
// (int)bi[12]&amp;0xff;
// System.out.println("Planes is :"+nplanes);
nbitcount = constructShort(bi, 14); //(((int)bi[15]&amp;0xff)&lt;&lt;8) |
// (int)bi[14]&amp;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 &lt; bh.nheight; j++)
{
for (int i = 0; i &lt; 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 &lt; bh.nheight; j++)
{
for (int i = 0; i &lt; 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 &gt; 0)
{
nNumColors = bh.nclrused;
}
else
{
nNumColors = (1 &amp; 0xff) &lt;&lt; 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) &amp; ~31) &gt;&gt; 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 &lt; 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 &lt; bh.nheight; j8++)
{
for (int i8 = 0; i8 &lt; bh.nwidth; i8++)
{
ndata8[bh.nwidth * (bh.nheight - j8 - 1) + i8] =
npalette[((int) bdata[nindex8] &amp; 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 &gt;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++){

閱讀全文

與java讀取bmp相關的資料

熱點內容
cad最下面的一排命令都什麼意思 瀏覽:456
pythonimportcpp 瀏覽:850
W10的系統怎麼給U盤加密 瀏覽:370
華為手機代碼編程教學入門 瀏覽:762
和彩雲沒會員怎樣解壓 瀏覽:634
androidimageview保存 瀏覽:387
新買店鋪什麼伺服器 瀏覽:883
文件夾能直接刻錄嗎 瀏覽:493
androidxmpp刪除好友 瀏覽:969
javac哪個前景好 瀏覽:427
中華英才網app為什麼不能搜索了 瀏覽:660
伺服器域名是什麼意思 瀏覽:52
Linux導出mysql命令 瀏覽:159
無詐建鄴是什麼app 瀏覽:228
python中的雙色球 瀏覽:167
python解釋器里如何換行 瀏覽:412
python編寫格式 瀏覽:576
用python做出來的軟體 瀏覽:469
伺服器指示燈代表什麼 瀏覽:702
做一個單片機銷售需要知識 瀏覽:777