JAVA 图片大小设置

JAVA 图片大小设置,第1张

应该是路径问题。

把你要显示的放在你的Test类里,

再把 :con = new ImageIcon("D:\\java程序\\1jpg");

改成 : icon = new ImageIcon("/1jpg");

你试试,看行不。

给你一个java的,自己改改

import javaawt;

import javaawtimage;

import javautilRandom;

import javaio;

import javaxswing;

public class LoadImage {

/

@param args

/

public static void main(String[] args) {

String myreadline = "";

//定义一个String类型的变量,用来每次读取一行

try {

FileReader fr = new FileReader("data/imagelisttxt");//创建FileReader对象,用来读取字符流

BufferedReader br = new BufferedReader(fr); //缓冲指定文件的输入

FileWriter fw = new FileWriter("data/17d_resulttxt");//创建FileWriter对象,用来写入字符流

BufferedWriter bw = new BufferedWriter(fw); //将缓冲对文件的输出

while (brready()) {

myreadline = brreadLine();//读取一行

BufferedImage image = toBufferedImage(new ImageIcon("data/Image/"+myreadline)getImage());

int height = imagegetHeight();

int width = imagegetWidth();

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;

//Systemoutprintln("Height=" + height + ", Width=" + width);

//Random ran = new Random();

//int x = rannextInt(width), y = rannextInt(height);

for (int y=0;y<height;y++) {

for (int x=0;x<width;x++) {

Color color = new Color(imagegetRGB(x, y));

if(colorgetRed()<=15)

r1++;

if(colorgetRed()>15 && colorgetRed()<=30)

r2++;

if(colorgetRed()>30 && colorgetRed()<=45)

r3++;

if(colorgetRed()>45 && colorgetRed()<=60)

r4++;

if(colorgetRed()>60 && colorgetRed()<=75)

r5++;

if(colorgetRed()>75 && colorgetRed()<=90)

r6++;

if(colorgetRed()>90 && colorgetRed()<=105)

r7++;

if(colorgetRed()>105 && colorgetRed()<=120)

r8++;

if(colorgetRed()>120 && colorgetRed()<=135)

r9++;

if(colorgetRed()>135 && colorgetRed()<=150)

r10++;

if(colorgetRed()>150 && colorgetRed()<=165)

r11++;

if(colorgetRed()>165 && colorgetRed()<=180)

r12++;

if(colorgetRed()>180 && colorgetRed()<=195)

r13++;

if(colorgetRed()>195 && colorgetRed()<=210)

r14++;

if(colorgetRed()>210 && colorgetRed()<=225)

r15++;

if(colorgetRed()>225 && colorgetRed()<=240)

r16++;

if(colorgetRed()>240 && colorgetRed()<=255)

r17++;

if(colorgetGreen()<=15)

g1++;

if(colorgetGreen()>15 && colorgetGreen()<=30)

g2++;

if(colorgetGreen()>30 && colorgetGreen()<=45)

g3++;

if(colorgetGreen()>45 && colorgetGreen()<=60)

g4++;

if(colorgetGreen()>60 && colorgetGreen()<=75)

g5++;

if(colorgetGreen()>75 && colorgetGreen()<=90)

g6++;

if(colorgetGreen()>90 && colorgetGreen()<=105)

g7++;

if(colorgetGreen()>105 && colorgetGreen()<=120)

g8++;

if(colorgetGreen()>120 && colorgetGreen()<=135)

g9++;

if(colorgetGreen()>135 && colorgetGreen()<=150)

g10++;

if(colorgetGreen()>150 && colorgetGreen()<=165)

g11++;

if(colorgetGreen()>165 && colorgetGreen()<=180)

g12++;

if(colorgetGreen()>180 && colorgetGreen()<=195)

g13++;

if(colorgetGreen()>195 && colorgetGreen()<=210)

g14++;

if(colorgetGreen()>210 && colorgetGreen()<=225)

g15++;

if(colorgetGreen()>225 && colorgetGreen()<=240)

g16++;

if(colorgetGreen()>240 && colorgetGreen()<=255)

g17++;

if(colorgetBlue()<=15)

b1++;

if(colorgetBlue()>15 && colorgetBlue()<=30)

b2++;

if(colorgetBlue()>30 && colorgetBlue()<=45)

b3++;

if(colorgetBlue()>45 && colorgetBlue()<=60)

b4++;

if(colorgetBlue()>60 && colorgetBlue()<=75)

b5++;

if(colorgetBlue()>75 && colorgetBlue()<=90)

b6++;

if(colorgetBlue()>90 && colorgetBlue()<=105)

b7++;

if(colorgetBlue()>105 && colorgetBlue()<=120)

b8++;

if(colorgetBlue()>120 && colorgetBlue()<=135)

b9++;

if(colorgetBlue()>135 && colorgetBlue()<=150)

b10++;

if(colorgetBlue()>150 && colorgetBlue()<=165)

b11++;

if(colorgetBlue()>165 && colorgetBlue()<=180)

b12++;

if(colorgetBlue()>180 && colorgetBlue()<=195)

b13++;

if(colorgetBlue()>195 && colorgetBlue()<=210)

b14++;

if(colorgetBlue()>210 && colorgetBlue()<=225)

b15++;

if(colorgetBlue()>225 && colorgetBlue()<=240)

b16++;

if(colorgetBlue()>240 && colorgetBlue()<=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;

//Systemoutprintln("rect " + rgb1 + " " + rgb2 + " " + rgb3);

bwwrite("rect " + rgb1 + " " + rgb2 + " " + rgb3 + " " + rgb4 + " " + rgb5 + " " + rgb6 + " " + rgb7 + " " + rgb8 + " " + rgb9 + " " + rgb10 + " " + rgb11 + " " + rgb12 + " " + rgb13 + " " + rgb14 + " " + rgb15 + " " + rgb16 + " " + rgb17); //写入文件

bwnewLine();

//Systemoutprintln(myreadline);//在屏幕上输出

}

bwflush(); //刷新该流的缓冲

bwclose();

brclose();

fwclose();

brclose();

frclose();

} catch (IOException e) {

eprintStackTrace();

}

}

// 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 pixels; for 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 = TransparencyOPAQUE;

if (hasAlpha) {

transparency = TransparencyBITMASK;

}

// Create the buffered image

GraphicsDevice gs = gegetDefaultScreenDevice();

GraphicsConfiguration gc = gsgetDefaultConfiguration();

bimage = gccreateCompatibleImage(imagegetWidth(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 = BufferedImageTYPE_INT_RGB;

if (hasAlpha) {

type = BufferedImageTYPE_INT_ARGB;

}

bimage = new BufferedImage(imagegetWidth(null), image

getHeight(null), type);

}

// Copy image to buffered image

Graphics g = bimagecreateGraphics();

// Paint the image onto the buffered image

gdrawImage(image, 0, 0, null);

gdispose();

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 bimagegetColorModel()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 {

pggrabPixels();

} catch (InterruptedException e) {

}

// Get the image's color model

ColorModel cm = pggetColorModel();

return cmhasAlpha();

}

}

height == -1; 也就是没有。

使用 imggetHeight(null);

加载到内存中,可获真实高度,否则得到img对象,但没加载到内存就会等于 -1;

而toolkit并没有加载到内存,只有调用gdrawImage才把加载到内存。

这种方式加载的并没有将加载到内存中,可以换另外一种方式书写

URL u = GameUtilclassgetClassLoader()getResource(path);

BufferedImage img = null;

try{

img = ImageIOread(u);

}catch(IOException e){

eprintStackTrace();

}

字符串的宽高长度是木有的

你说的这个应该是在某个地方画出来后那么这里的字就会有宽高了比如在你桌面上面的字符串,或者游戏里面那些字是画出来的那么也有了宽度

这个是有了这个Font 这个字体类才有字符串的宽高概念出现

fontgetHeight(); //得到字高

fontstringWidth(Strng s); //得到s字符串的总宽

我是做手机游戏有时候会用的计算字符串的宽高所以不知道你想知道是不是这个

占内存容量计算公式为:

所占内存大小 = 长度(像素) 宽度(像素) 一个像素所占内存空间(单位:字节

一般地,一个象素所占内存空间根据机器颜色数(专业词汇叫色深Color Depth)来决定:(1)Nokia 老S40机器 颜色数为4096色,就是2^12, 一个象素所占内存空间为15个字节;(2)Nokia S40新版 机器和 S60机器 颜色数为65536色,就是2^16, 一个象素所占内存空间为2个字节;有些机器颜色数为26w色,就是2^18=22字节,1600w = 2^24=3个字节,一个象素所占内存空间为4个字节。

ps:专业名词“8位”是指所能表现的颜色深度:一个8位图像仅最多只能支持256(2^8)种不同颜色,1个字节。

以上就是关于JAVA 图片大小设置全部的内容,包括:JAVA 图片大小设置、c/c++/java/c#读取png图片、java中为什么img.getHeight(null)总是-1;等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9443328.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存