当前位置: 技术问答>java相关
Java 都支持那些图形格式?为什么连 ICON 和 BMP 这样通用的格式都不自带支持!!!!!
来源: 互联网 发布时间:2015-03-12
本文导语: Icon img_1 = new ImageIcon("img.jpg"); Icon img_2 = new ImageIcon("img.bmp"); Icon img_3 = new ImageIcon("img.ioc"); JLabel lab_1 = new JLabel(img_1); JLabel lab_2 = new JLabel(img_2); JLabel lab_3 = new JLabel(img_3); 结果只有第一个标签可以正常...
Icon img_1 = new ImageIcon("img.jpg");
Icon img_2 = new ImageIcon("img.bmp");
Icon img_3 = new ImageIcon("img.ioc");
JLabel lab_1 = new JLabel(img_1);
JLabel lab_2 = new JLabel(img_2);
JLabel lab_3 = new JLabel(img_3);
结果只有第一个标签可以正常的显示,后两个却什么也没有?????
真搞不懂是什么原因??
请各位高手指点!!!
这几个图像都在同一个路径下的,不会出现路径的问题!
Icon img_2 = new ImageIcon("img.bmp");
Icon img_3 = new ImageIcon("img.ioc");
JLabel lab_1 = new JLabel(img_1);
JLabel lab_2 = new JLabel(img_2);
JLabel lab_3 = new JLabel(img_3);
结果只有第一个标签可以正常的显示,后两个却什么也没有?????
真搞不懂是什么原因??
请各位高手指点!!!
这几个图像都在同一个路径下的,不会出现路径的问题!
|
通用的JPG,GIF!
import java.awt.*;
import java.io.*;
import java.awt.image.*;
public class BMPFile extends Component
{
//--- Private constants
private final static int BITMAPFILEHEADER_SIZE = 14;
private final static int BITMAPINFOHEADER_SIZE = 40;
//--- Private variable declaration
//--- Bitmap file header
private byte bitmapFileHeader[] = new byte[14];
private byte bfType[] = { 'B', 'M' };
private int bfSize = 0;
private int bfReserved1 = 0;
private int bfReserved2 = 0;
private int bfOffBits = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;
//--- Bitmap info header
private byte bitmapInfoHeader[] = new byte[40];
private int biSize = BITMAPINFOHEADER_SIZE;
private int biWidth = 0;
private int biHeight = 0;
private int biPlanes = 1;
private int biBitCount = 24;
private int biCompression = 0;
private int biSizeImage = 0x030000;
private int biXPelsPerMeter = 0x0;
private int biYPelsPerMeter = 0x0;
private int biClrUsed = 0;
private int biClrImportant = 0;
//--- Bitmap raw data
private int bitmap[];
//--- File section
private FileOutputStream fo;
//--- Default constructor
public BMPFile ()
{
}
public void saveBitmap(String parFilename, Image parImage, int parWidth, int parHeight)
{
try
{
fo = new FileOutputStream(parFilename);
save(parImage, parWidth, parHeight);
fo.close();
}
catch(Exception saveEx)
{
saveEx.printStackTrace();
}
}
/*
* The saveMethod is the main method of the process. This method
* will call the convertImage method to convert the memory image to
* a byte array; method writeBitmapFileHeader creates and writes
* the bitmap file header; writeBitmapInfoHeader creates the
* information header; and writeBitmap writes the image.
*
*/
private void save(Image parImage, int parWidth, int parHeight)
{
try
{
convertImage(parImage, parWidth, parHeight);
writeBitmapFileHeader();
writeBitmapInfoHeader();
writeBitmap();
}
catch(Exception saveEx)
{
saveEx.printStackTrace();
}
}
/*
* convertImage converts the memory image to the bitmap format (BRG).
* It also computes some information for the bitmap info header.
*
*/
private boolean convertImage(Image parImage, int parWidth, int parHeight)
{
int pad;
bitmap = new int[parWidth * parHeight];
PixelGrabber pg = new PixelGrabber(parImage, 0, 0, parWidth, parHeight, bitmap, 0, parWidth);
try
{
pg.grabPixels();
}
catch(InterruptedException e)
{
e.printStackTrace();
return (false);
}
pad = (4 - ((parWidth * 3) % 4)) * parHeight;
biSizeImage = ((parWidth * parHeight) * 3) + pad;
bfSize = biSizeImage + BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;
biWidth = parWidth;
biHeight = parHeight;
return (true);
}
/*
* writeBitmap converts the image returned from the pixel grabber to
* the format required. Remember: scan lines are inverted in
* a bitmap file!
*
* Each scan line must be padded to an even 4-byte boundary.
*/
private void writeBitmap()
{
int size;
int value;
int j;
int i;
int rowCount;
int rowIndex;
int lastRowIndex;
int pad;
int padCount;
byte rgb[] = new byte[3];
size = (biWidth * biHeight) - 1;
pad = 4 - ((biWidth * 3) % 4);
if(pad == 4)// > 16) & 0xFF);
fo.write(rgb);
if(rowCount == biWidth)
{
padCount += pad;
for(i = 1; i > 8) & 0x00FF);
return (retValue);
}
/*
*
* intToDWord converts an int to a double word, where the return
* value is stored in a 4-byte array.
*
*/
private byte[] intToDWord(int parValue)
{
byte retValue[] = new byte[4];
retValue[0] = (byte)(parValue & 0x00FF);
retValue[1] = (byte)((parValue >> 8) & 0x000000FF);
retValue[2] = (byte)((parValue >> 16) & 0x000000FF);
retValue[3] = (byte)((parValue >> 24) & 0x000000FF);
return (retValue);
}
}
import java.awt.*;
import java.io.*;
import java.awt.image.*;
public class BMPFile extends Component
{
//--- Private constants
private final static int BITMAPFILEHEADER_SIZE = 14;
private final static int BITMAPINFOHEADER_SIZE = 40;
//--- Private variable declaration
//--- Bitmap file header
private byte bitmapFileHeader[] = new byte[14];
private byte bfType[] = { 'B', 'M' };
private int bfSize = 0;
private int bfReserved1 = 0;
private int bfReserved2 = 0;
private int bfOffBits = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;
//--- Bitmap info header
private byte bitmapInfoHeader[] = new byte[40];
private int biSize = BITMAPINFOHEADER_SIZE;
private int biWidth = 0;
private int biHeight = 0;
private int biPlanes = 1;
private int biBitCount = 24;
private int biCompression = 0;
private int biSizeImage = 0x030000;
private int biXPelsPerMeter = 0x0;
private int biYPelsPerMeter = 0x0;
private int biClrUsed = 0;
private int biClrImportant = 0;
//--- Bitmap raw data
private int bitmap[];
//--- File section
private FileOutputStream fo;
//--- Default constructor
public BMPFile ()
{
}
public void saveBitmap(String parFilename, Image parImage, int parWidth, int parHeight)
{
try
{
fo = new FileOutputStream(parFilename);
save(parImage, parWidth, parHeight);
fo.close();
}
catch(Exception saveEx)
{
saveEx.printStackTrace();
}
}
/*
* The saveMethod is the main method of the process. This method
* will call the convertImage method to convert the memory image to
* a byte array; method writeBitmapFileHeader creates and writes
* the bitmap file header; writeBitmapInfoHeader creates the
* information header; and writeBitmap writes the image.
*
*/
private void save(Image parImage, int parWidth, int parHeight)
{
try
{
convertImage(parImage, parWidth, parHeight);
writeBitmapFileHeader();
writeBitmapInfoHeader();
writeBitmap();
}
catch(Exception saveEx)
{
saveEx.printStackTrace();
}
}
/*
* convertImage converts the memory image to the bitmap format (BRG).
* It also computes some information for the bitmap info header.
*
*/
private boolean convertImage(Image parImage, int parWidth, int parHeight)
{
int pad;
bitmap = new int[parWidth * parHeight];
PixelGrabber pg = new PixelGrabber(parImage, 0, 0, parWidth, parHeight, bitmap, 0, parWidth);
try
{
pg.grabPixels();
}
catch(InterruptedException e)
{
e.printStackTrace();
return (false);
}
pad = (4 - ((parWidth * 3) % 4)) * parHeight;
biSizeImage = ((parWidth * parHeight) * 3) + pad;
bfSize = biSizeImage + BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;
biWidth = parWidth;
biHeight = parHeight;
return (true);
}
/*
* writeBitmap converts the image returned from the pixel grabber to
* the format required. Remember: scan lines are inverted in
* a bitmap file!
*
* Each scan line must be padded to an even 4-byte boundary.
*/
private void writeBitmap()
{
int size;
int value;
int j;
int i;
int rowCount;
int rowIndex;
int lastRowIndex;
int pad;
int padCount;
byte rgb[] = new byte[3];
size = (biWidth * biHeight) - 1;
pad = 4 - ((biWidth * 3) % 4);
if(pad == 4)// > 16) & 0xFF);
fo.write(rgb);
if(rowCount == biWidth)
{
padCount += pad;
for(i = 1; i > 8) & 0x00FF);
return (retValue);
}
/*
*
* intToDWord converts an int to a double word, where the return
* value is stored in a 4-byte array.
*
*/
private byte[] intToDWord(int parValue)
{
byte retValue[] = new byte[4];
retValue[0] = (byte)(parValue & 0x00FF);
retValue[1] = (byte)((parValue >> 8) & 0x000000FF);
retValue[2] = (byte)((parValue >> 16) & 0x000000FF);
retValue[3] = (byte)((parValue >> 24) & 0x000000FF);
return (retValue);
}
}
|
jpeg,gif!
|
yes,only jpeg and gif