#include <stdio.h>
int main()
{
int driver=0,mode=0
initgraph(&driver,&mode,"")
IMAGE img_1
loadimage(&img_1, _T("1.jpg"))
putimage(20, 20,&img_1)
getchar()
closegraph()
}
给你一个java的,自己改改import java.awt.*
import java.awt.image.*
import java.util.Random
import java.io.*
import javax.swing.*
public class LoadImage {
/**
* @param args
*/
public static void main(String[] args) {
String myreadline = ""
//定义一个String类型的变量,用来每次读取一行
try {
FileReader fr = new FileReader("data/imagelist.txt")//创建FileReader对象,用来读取字符流
BufferedReader br = new BufferedReader(fr) //缓冲指定文件的输入
FileWriter fw = new FileWriter("data/17d_result.txt")//创建FileWriter对象,用来写入字符流
BufferedWriter bw = new BufferedWriter(fw) //将缓冲对文件的输出
while (br.ready()) {
myreadline = br.readLine()//读取一行
BufferedImage image = toBufferedImage(new ImageIcon("data/Image/"+myreadline).getImage())
int height = image.getHeight()
int width = image.getWidth()
int r1=0, r2=0, r3=0, r4=0, r5=0, r6=0, r7=0, r8=0, r9=0, r10=0, r11=0, r12=0, r13=0, r14=0, r15=0, r16=0, r17=0
int g1=0, g2=0, g3=0, g4=0, g5=0, g6=0, g7=0, g8=0, g9=0, g10=0, g11=0, g12=0, g13=0, g14=0, g15=0, g16=0, g17=0
int b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0, b8=0, b9=0, b10=0, b11=0, b12=0, b13=0, b14=0, b15=0, b16=0, b17=0
int rgb1=0, rgb2=0, rgb3=0, rgb4=0, rgb5=0, rgb6=0, rgb7=0, rgb8=0, rgb9=0, rgb10=0, rgb11=0, rgb12=0, rgb13=0, rgb14=0, rgb15=0, rgb16=0, rgb17=0
//System.out.println("Height=" + height + ", Width=" + width)
//Random ran = new Random()
//int x = ran.nextInt(width), y = ran.nextInt(height)
for (int y=0y<heighty++) {
for (int x=0x<widthx++) {
Color color = new Color(image.getRGB(x, y))
if(color.getRed()<=15)
r1++
if(color.getRed()>15 &&color.getRed()<=30)
r2++
if(color.getRed()>30 &&color.getRed()<=45)
r3++
if(color.getRed()>45 &&color.getRed()<=60)
r4++
if(color.getRed()>60 &&color.getRed()<=75)
r5++
if(color.getRed()>75 &&color.getRed()<=90)
r6++
if(color.getRed()>90 &&color.getRed()<=105)
r7++
if(color.getRed()>105 &&color.getRed()<=120)
r8++
if(color.getRed()>120 &&color.getRed()<=135)
r9++
if(color.getRed()>135 &&color.getRed()<=150)
r10++
if(color.getRed()>150 &&color.getRed()<=165)
r11++
if(color.getRed()>165 &&color.getRed()<=180)
r12++
if(color.getRed()>180 &&color.getRed()<=195)
r13++
if(color.getRed()>195 &&color.getRed()<=210)
r14++
if(color.getRed()>210 &&color.getRed()<=225)
r15++
if(color.getRed()>225 &&color.getRed()<=240)
r16++
if(color.getRed()>240 &&color.getRed()<=255)
r17++
if(color.getGreen()<=15)
g1++
if(color.getGreen()>15 &&color.getGreen()<=30)
g2++
if(color.getGreen()>30 &&color.getGreen()<=45)
g3++
if(color.getGreen()>45 &&color.getGreen()<=60)
g4++
if(color.getGreen()>60 &&color.getGreen()<=75)
g5++
if(color.getGreen()>75 &&color.getGreen()<=90)
g6++
if(color.getGreen()>90 &&color.getGreen()<=105)
g7++
if(color.getGreen()>105 &&color.getGreen()<=120)
g8++
if(color.getGreen()>120 &&color.getGreen()<=135)
g9++
if(color.getGreen()>135 &&color.getGreen()<=150)
g10++
if(color.getGreen()>150 &&color.getGreen()<=165)
g11++
if(color.getGreen()>165 &&color.getGreen()<=180)
g12++
if(color.getGreen()>180 &&color.getGreen()<=195)
g13++
if(color.getGreen()>195 &&color.getGreen()<=210)
g14++
if(color.getGreen()>210 &&color.getGreen()<=225)
g15++
if(color.getGreen()>225 &&color.getGreen()<=240)
g16++
if(color.getGreen()>240 &&color.getGreen()<=255)
g17++
if(color.getBlue()<=15)
b1++
if(color.getBlue()>15 &&color.getBlue()<=30)
b2++
if(color.getBlue()>30 &&color.getBlue()<=45)
b3++
if(color.getBlue()>45 &&color.getBlue()<=60)
b4++
if(color.getBlue()>60 &&color.getBlue()<=75)
b5++
if(color.getBlue()>75 &&color.getBlue()<=90)
b6++
if(color.getBlue()>90 &&color.getBlue()<=105)
b7++
if(color.getBlue()>105 &&color.getBlue()<=120)
b8++
if(color.getBlue()>120 &&color.getBlue()<=135)
b9++
if(color.getBlue()>135 &&color.getBlue()<=150)
b10++
if(color.getBlue()>150 &&color.getBlue()<=165)
b11++
if(color.getBlue()>165 &&color.getBlue()<=180)
b12++
if(color.getBlue()>180 &&color.getBlue()<=195)
b13++
if(color.getBlue()>195 &&color.getBlue()<=210)
b14++
if(color.getBlue()>210 &&color.getBlue()<=225)
b15++
if(color.getBlue()>225 &&color.getBlue()<=240)
b16++
if(color.getBlue()>240 &&color.getBlue()<=255)
b17++
}
}
rgb1 = r1 + g1 + b1
rgb2 = r2 + g2 + b2
rgb3 = r3 + g3 + b3
rgb4 = r4 + g4 + b4
rgb5 = r5 + g5 + b5
rgb6 = r6 + g6 + b6
rgb7 = r7 + g7 + b7
rgb8 = r8 + g8 + b8
rgb9 = r9 + g9 + b9
rgb10 = r10 + g10 + b10
rgb11 = r11 + g11 + b11
rgb12 = r12 + g12 + b12
rgb13 = r13 + g13 + b13
rgb14 = r14 + g14 + b14
rgb15 = r15 + g15 + b15
rgb16 = r16 + g16 + b16
rgb17 = r17 + g17 + b17
//System.out.println("rect " + rgb1 + " " + rgb2 + " " + rgb3)
bw.write("rect " + rgb1 + " " + rgb2 + " " + rgb3 + " " + rgb4 + " " + rgb5 + " " + rgb6 + " " + rgb7 + " " + rgb8 + " " + rgb9 + " " + rgb10 + " " + rgb11 + " " + rgb12 + " " + rgb13 + " " + rgb14 + " " + rgb15 + " " + rgb16 + " " + rgb17)//写入文件
bw.newLine()
//System.out.println(myreadline)//在屏幕上输出
}
bw.flush() //刷新该流的缓冲
bw.close()
br.close()
fw.close()
br.close()
fr.close()
} catch (IOException e) {
e.printStackTrace()
}
}
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image
}
// Determine if the image has transparent pixelsfor this method's
// implementation, see e661 Determining If an Image Has Transparent
// Pixels
boolean hasAlpha = hasAlpha(image)
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment()
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE
if (hasAlpha) {
transparency = Transparency.BITMASK
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice()
GraphicsConfiguration gc = gs.getDefaultConfiguration()
bimage = gc.createCompatibleImage(image.getWidth(null), image
.getHeight(null), transparency)
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB
}
bimage = new BufferedImage(image.getWidth(null), image
.getHeight(null), type)
}
// Copy image to buffered image
Graphics g = bimage.createGraphics()
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null)
g.dispose()
return bimage
}
// This method returns true if the specified image has transparent pixels
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image
return bimage.getColorModel().hasAlpha()
}
// Use a pixel grabber to retrieve the image's color model
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false)
try {
pg.grabPixels()
} catch (InterruptedException e) {
}
// Get the image's color model
ColorModel cm = pg.getColorModel()
return cm.hasAlpha()
}
}
添加62字节的头信息就可以了!第一部分为位图文件头BITMAPFILEHEADER,是一个结构,其定义如下:
typedefstructtagBITMAPFILEHEADER{
WORD bfType
DWORD bfSize
WORD bfReserved1
WORD bfReserved2
DWORD bfOffBits
} BITMAPFILEHEADER
这个结构的长度是固定的,为14个字节(WORD为无符号16位整数,DWORD为无符号32位整数),各个域的说明如下:
bfType
指定文件类型,必须是0x424D,即字符串 "BM ",也就是说所有.bmp文件的头两个字节都是 "BM "
bfSize
指定文件大小,包括这14个字节
bfReserved1,bfReserved2
为保留字,不用考虑
bfOffBits
为从文件头到实际的位图数据的偏移字节数,即图3中前三个部分的长度之和。
第二部分为位图信息头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
这个结构的长度是固定的,为40个字节(WORD为无符号16位整数,DWORD无符
号32位整数,LONG为32位整数),各个域的说明如下:
biSize
指定这个结构的长度,为40
biWidth
指定图象的宽度,单位是象素
biHeight
指定图象的高度,单位是象素
biPlanes
必须是1,不用考虑
biBitCount
指定表示颜色时要用到的位数,常用的值为1(黑白二色图),4(16色图),8(256色),24(真彩色图)(新的.bmp格式支持32位色,这里就不做讨论了)。
biCompression
指定位图是否压缩,有效的值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS(都是一些Windows定义好的常量)。要说明的是,Windows位图可以采用RLE4,和RLE8的压缩格式,但用的不多。我们今后所讨论的只有第一种不压缩的情况,即biCompression 为BI_RGB的情况。
biSizeImage
指定实际的位图数据占用的字节数,其实也可以从以下的公式中计算出来:
biSizeImage=biWidth '*biHeight
要注意的是:上述公式中的biWidth '必须是4的整倍数(所以不是biWidth,而是biWidth ',表示大于或等于biWidth的,离4最近的整倍数。举个例子,如果biWidth=240,则biWidth '=240;如果biWidth=241,biWidth '=244)如果biCompression为BI_RGB,则该项可能为零
biXPelsPerMeter
指定目标设备的水平分辨率,单位是每米的象素个数,关于分辨率的概念,我们将在打印部分详细介绍。
biYPelsPerMeter
指定目标设备的垂直分辨率,单位同上。
biClrUsed
指定本图象实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次方。
biClrImportant
指定本图象中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。
第三部分为调色板(Palette),当然,这里是对那些需要调色板的位图文件而言的。有些位图,如真彩色图,前面已经讲过,是不需要调色板的,BITMAPINFOHEADER后直接是位图数据。
调色板实际上是一个数组,共有biClrUsed个元素(如果该值为零,则有2的biBitCount次方个元素)。数组中每个元素的类型是一个RGBQUAD结构,占4个字节,其定义如下:
typedef struct tagRGBQUAD{
BYTE rgbBlue//该颜色的蓝色分量
BYTE rgbGreen//该颜色的绿色分量
BYTE rgbRed//该颜色的红色分量
BYTE rgbReserved//保留值
} RGBQUAD
第四部分就是实际的图象数据了。对于用到调色板的位图,图象数据就是该像素颜在调色板中的索引值,对于真彩色图,图象数据就是实际的R,G,B值。下面就2色,16色,256色位图和真彩色位图分别介绍。
对于2色位图,用1位就可以表示该像素的颜色(一般0表示黑,1表示白),所以一个字节可以表示8个像素。
对于16色位图,用4位可以表示一个像素的颜色,所以一个字节可以表示2个像素。
对于256色位图,一个字节刚好可以表示1个像素。
对于真彩色图,三个字节才能表示1个像素,哇噻,好费空间呀,没办法,谁叫你想让图的颜色显得更亮丽呢,有得必有失嘛。
要注意两点:
1.每一行的字节数必须是4的整倍数,如果不是,则需要补齐。这在前面介绍biSizeImage时已经提到了。
2.一般来说,.BMP文件的数据从下到上,从左到右的。也就是说,从文件中最先读到的是图象最下面一行的左边第一个像素,然后是左边第二个像素…接下来是倒数第二行左边第一个像素,左边第二个像素…依次类推,最后得到的是最上面一行的最右一个像素。
了解这些以后呢 可以通过以二进制的方式“rb”打开你想读取的png图片
FILE *fp
if(!(fp=open(“filename”,rb)))
printf(“open file error”);
打开成功后就可以通过文件指针读取文件内容了。