新建一个image实例,如何从文件读入图片赋值给他,求代码?? 谢了 image 或者 bufferedimage xx;"c:\\a.jpg"; 是待读入的图片.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主【Java_gannbare】截止到2008-07-30 07:28:44的历史汇总数据(不包括此帖):发帖的总数量:9 发帖的总分数:250 每贴平均分数:27 回帖的总数量:9 得分贴总数量:0 回帖的得分率:0% 结贴的总数量:7 结贴的总分数:190 无满意结贴数:0 无满意结贴分:0 未结的帖子数:2 未结的总分数:60 结贴的百分比:77.78 % 结分的百分比:76.00 % 无满意结贴率:0.00 % 无满意结分率:0.00 % 楼主加油取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=Java_gannbare xx = new ImageIcon ( "c:\\a.jpg" ).getImage() ; 应该有你想要的吧import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Image;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.swing.Icon;import javax.swing.ImageIcon;import org.apache.log4j.Logger;import org.apache.struts.tiles.PathAttribute;import com.media.util.PathUtil;import com.media.util.Utility;import com.media.util.file.WriteLogoSyncFile;import com.sun.image.codec.jpeg.ImageFormatException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CreateTextImg { String bgImgName = "ad_logo_bg.png"; float quantity = 0.5f ; Logger log = Logger.getLogger( this.getClass().getName() ) ; public BufferedImage createImg(String content, String footer, int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g = image.createGraphics();// g.setColor(new Color(0, 0, 0)); g.fillRect(0, 0, width, height); g.setFont(new Font("宋体", Font.PLAIN, 12));// g.setColor(new Color(0, 0, 0)); g.setColor(new Color(255, 255, 255)); String[] ary = content.split("\n"); System.out.println(content); g.drawString(ary[0], 3, 15); if (ary.length > 1) { g.drawString(ary[1], 3, 30); } g.dispose(); return image; } public BufferedImage createImgByBg(String content, String footer, int width, int height) { String path = new PathUtil().getAdLogoPath() + "/" + bgImgName; BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR); File F = new File(path); try { image = ImageIO.read(F); } catch (Exception e) { e.printStackTrace() ; } Graphics g = image.createGraphics(); // g.setColor(new Color(0, 0, 0)); // g.fillRect(0, 0, width, height); // g.setFont(new Font("宋体", Font.PLAIN, 12));// g.setFont(new Font("方正艺黑简体", Font.PLAIN, 12)); g.setFont(new Font("宋体", Font.PLAIN, 12));// g.setColor(new Color(0xff, 0xff, 0xff)); g.setColor(new Color(0, 0, 0)); String[] ary = null; Utility.println( "content:" + content , log) ; if (content.indexOf("\n") > -1) { ary = content.split("\n"); } else { /* * if ( content.getBytes().length > 20 ){ ary = new String[2] ; * ary[0] = this.subStrBye(content, 0, 20 ) ; System.out.println( * this.subStrBye(content, 0, 20 ) ); System.out.println( * this.subStrBye(content, 0, 21 ) ); ary[1] = * this.subStrBye(content, 20, 20 ) ; }else{ ary = new String[1] ; * ary[0] = content ; } */ ary = new String[1]; ary[0] = content; } // width = image.getWidth() ; FontMetrics FM = g.getFontMetrics(); int tWidth = FM.stringWidth(ary[0]); width = image.getWidth(); Utility.println( "ary:" + ary.length , log) ; if (ary.length > 1) { Utility.println( "ary0:" + ary[0], log) ; Utility.println( "width:" + width, log) ; Utility.println( "tWidth:" + tWidth, log) ; g.drawString(ary[0], (width - tWidth) / 2, 15); tWidth = FM.stringWidth( ary[1] ) ; g.drawString(ary[1], (width - tWidth) / 2, 30); } else { g.drawString(ary[0], (width - tWidth) / 2, 25); } g.dispose(); return image; } public void saveAsImage( String path , String destPath){ File f = new File(path); BufferedImage image=null; try { image = ImageIO.read(f ); } catch (Exception e) { e.printStackTrace() ; } this.saveImg(destPath, image , quantity) ; } public void saveImg(String path, BufferedImage bufdImg,float quantity ) { FileOutputStream fos = null; File f = null; try { f = new File(path); } catch (Exception e2) { e2.printStackTrace(); } try { fos = new FileOutputStream(f); } catch (IOException e) { e.printStackTrace(); } JPEGImageEncoder encoder = null; encoder = JPEGCodec.createJPEGEncoder(fos); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufdImg); param.setQuality( quantity, true); try { encoder.encode(bufdImg, param); encoder.encode(bufdImg); } catch (ImageFormatException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } try { fos.flush(); fos.close(); } catch (IOException e3) { e3.printStackTrace(); } fos = null; } public void scaledAndCreateImg(File file, String filePath, int width, int height) throws Exception { double Ratio = 0.0; // 缩放比例 File F = new File(filePath); if (!F.isFile()) throw new Exception(F + " is not image file error in getFixedBoundIcon!"); BufferedImage Bi = ImageIO.read(F); if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) { if (Bi.getWidth() < width) { return; } Ratio = (new Integer(width)).doubleValue() / Bi.getWidth(); height = (int) (Bi.getHeight() * Ratio); File ThF = null; if (file == null) { ThF = new File(filePath); } else { ThF = file; } Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH); AffineTransformOp op = new AffineTransformOp(AffineTransform .getScaleInstance(Ratio, Ratio), null); Itemp = op.filter(Bi, null); try {// ImageIO.write((BufferedImage) Itemp, "png", ThF); this.saveImg(filePath, (BufferedImage)Itemp , (float)0.1 ) ; } catch (Exception ex) { } } } public void createImg(String path, String name, String ft, String clientId) { String footer = null; if (ft == null) { footer = "www.5emedia.com"; } else { footer = ft; } CreateTextImg cti = new CreateTextImg(); BufferedImage image = null; try { image = cti.createImgByBg(name, footer, 120, 50); } catch (Exception e) { // TODO: handle exception } WriteLogoSyncFile wfile = new WriteLogoSyncFile(); cti.saveImg(path, image , (float)0.5); wfile.write(path, clientId); } public String subStrBye(String str, int start, int len) { String s = null; byte[] bb = str.getBytes(); int srclen = bb.length - start > len ? len : bb.length - start; byte[] destb = new byte[srclen]; int destlen = 0; int cncount = 0; int j = 0; boolean run = false; int cn = 0; for (int i = start; i < bb.length; i++) { if (j + 1 > len) { if (cncount == 1) { destlen = srclen - 1; } break; } if (!run) { run = true; for (int k = 0; k < start; k++) { if (bb[k] < 0) cn++; } if (cn % 2 > 0) { i -= 2; destb = new byte[++srclen]; continue; } } else { run = true; } if (cncount == 2) cncount = 0; destb[j++] = bb[i]; if (bb[i] < 0) { cncount++; } } byte[] b = null; if (destlen > 0) { b = new byte[destlen]; System.arraycopy(destb, 0, b, 0, destlen); s = new String(b); } else { s = new String(destb); } return s; } public static void main(String[] args) { CreateTextImg cti = new CreateTextImg(); try { BufferedImage image = cti.createImgByBg("测试qwerta试\n测试测试", "footer", 100, 100); // BufferedImage image = // cti.createImgByBg("22222222222222222222222222", "footer", 500, // 100); cti.saveImg("F:/image.png", image,0.9f); } catch (Exception e) { e.printStackTrace(); } }} Toolkit kit = Toolkit.getDefaultToolkit(); Image img = kit.getImage("icon.gif"); 小白再来求助 关于api的一些问题 从JTable中直接修改数据,并存入数据库? 关于用户是否在线的处理,大家来讨论. 往父窗口的下拉框里加选项,就没有人会么,奇怪/ 在JAVA编程的过程中关于异常的使用方法,请前辈给予晚辈指点。 怎么打印排列组合的问题? 300分有请!!!帮我把这个程序改成多线程!!!!在线等待!!!!! 我现在用JBUILDER3在开发上落不落伍,也就是说,对于新型的应用,jbuilder3支持到什么程度? PreparedStatement设置参数问题 mouseClicked 与 mousePress有什么区别? js中传递函数的问题
楼主【Java_gannbare】截止到2008-07-30 07:28:44的历史汇总数据(不包括此帖):
发帖的总数量:9 发帖的总分数:250 每贴平均分数:27
回帖的总数量:9 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:7 结贴的总分数:190
无满意结贴数:0 无满意结贴分:0
未结的帖子数:2 未结的总分数:60
结贴的百分比:77.78 % 结分的百分比:76.00 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主加油
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=Java_gannbare
xx = new ImageIcon ( "c:\\a.jpg" ).getImage() ;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;import org.apache.log4j.Logger;
import org.apache.struts.tiles.PathAttribute;import com.media.util.PathUtil;
import com.media.util.Utility;
import com.media.util.file.WriteLogoSyncFile;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CreateTextImg { String bgImgName = "ad_logo_bg.png";
float quantity = 0.5f ;
Logger log = Logger.getLogger( this.getClass().getName() ) ;
public BufferedImage createImg(String content, String footer, int width,
int height) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
Graphics g = image.createGraphics();
// g.setColor(new Color(0, 0, 0));
g.fillRect(0, 0, width, height);
g.setFont(new Font("宋体", Font.PLAIN, 12));
// g.setColor(new Color(0, 0, 0));
g.setColor(new Color(255, 255, 255));
String[] ary = content.split("\n");
System.out.println(content);
g.drawString(ary[0], 3, 15);
if (ary.length > 1) {
g.drawString(ary[1], 3, 30);
}
g.dispose();
return image;
} public BufferedImage createImgByBg(String content, String footer, int width, int height) {
String path = new PathUtil().getAdLogoPath() + "/" + bgImgName;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR);
File F = new File(path);
try {
image = ImageIO.read(F);
} catch (Exception e) {
e.printStackTrace() ;
}
Graphics g = image.createGraphics();
// g.setColor(new Color(0, 0, 0));
// g.fillRect(0, 0, width, height);
// g.setFont(new Font("宋体", Font.PLAIN, 12));
// g.setFont(new Font("方正艺黑简体", Font.PLAIN, 12));
g.setFont(new Font("宋体", Font.PLAIN, 12));
// g.setColor(new Color(0xff, 0xff, 0xff));
g.setColor(new Color(0, 0, 0));
String[] ary = null;
Utility.println( "content:" + content , log) ;
if (content.indexOf("\n") > -1) {
ary = content.split("\n");
} else {
/*
* if ( content.getBytes().length > 20 ){ ary = new String[2] ;
* ary[0] = this.subStrBye(content, 0, 20 ) ; System.out.println(
* this.subStrBye(content, 0, 20 ) ); System.out.println(
* this.subStrBye(content, 0, 21 ) ); ary[1] =
* this.subStrBye(content, 20, 20 ) ; }else{ ary = new String[1] ;
* ary[0] = content ; }
*/
ary = new String[1];
ary[0] = content;
}
// width = image.getWidth() ;
FontMetrics FM = g.getFontMetrics();
int tWidth = FM.stringWidth(ary[0]);
width = image.getWidth();
Utility.println( "ary:" + ary.length , log) ;
if (ary.length > 1) {
Utility.println( "ary0:" + ary[0], log) ;
Utility.println( "width:" + width, log) ;
Utility.println( "tWidth:" + tWidth, log) ;
g.drawString(ary[0], (width - tWidth) / 2, 15);
tWidth = FM.stringWidth( ary[1] ) ;
g.drawString(ary[1], (width - tWidth) / 2, 30);
} else {
g.drawString(ary[0], (width - tWidth) / 2, 25);
}
g.dispose();
return image;
}
public void saveAsImage( String path , String destPath){
File f = new File(path);
BufferedImage image=null;
try {
image = ImageIO.read(f );
} catch (Exception e) {
e.printStackTrace() ;
}
this.saveImg(destPath, image , quantity) ;
}
public void saveImg(String path, BufferedImage bufdImg,float quantity ) {
FileOutputStream fos = null;
File f = null;
try {
f = new File(path);
} catch (Exception e2) {
e2.printStackTrace();
}
try {
fos = new FileOutputStream(f);
} catch (IOException e) {
e.printStackTrace();
}
JPEGImageEncoder encoder = null;
encoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufdImg);
param.setQuality( quantity, true);
try {
encoder.encode(bufdImg, param);
encoder.encode(bufdImg);
} catch (ImageFormatException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
fos.flush();
fos.close();
} catch (IOException e3) {
e3.printStackTrace();
}
fos = null;
} public void scaledAndCreateImg(File file, String filePath, int width,
int height) throws Exception {
double Ratio = 0.0; // 缩放比例
File F = new File(filePath);
if (!F.isFile())
throw new Exception(F
+ " is not image file error in getFixedBoundIcon!");
BufferedImage Bi = ImageIO.read(F);
if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
if (Bi.getWidth() < width) {
return;
}
Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
height = (int) (Bi.getHeight() * Ratio);
File ThF = null;
if (file == null) {
ThF = new File(filePath);
} else {
ThF = file;
}
Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
// ImageIO.write((BufferedImage) Itemp, "png", ThF);
this.saveImg(filePath, (BufferedImage)Itemp , (float)0.1 ) ;
} catch (Exception ex) {
}
}
} public void createImg(String path, String name, String ft, String clientId) {
String footer = null;
if (ft == null) {
footer = "www.5emedia.com";
} else {
footer = ft;
}
CreateTextImg cti = new CreateTextImg();
BufferedImage image = null;
try {
image = cti.createImgByBg(name, footer, 120, 50);
} catch (Exception e) {
// TODO: handle exception
}
WriteLogoSyncFile wfile = new WriteLogoSyncFile();
cti.saveImg(path, image , (float)0.5);
wfile.write(path, clientId);
} public String subStrBye(String str, int start, int len) {
String s = null;
byte[] bb = str.getBytes();
int srclen = bb.length - start > len ? len : bb.length - start;
byte[] destb = new byte[srclen];
int destlen = 0;
int cncount = 0;
int j = 0;
boolean run = false;
int cn = 0;
for (int i = start; i < bb.length; i++) {
if (j + 1 > len) {
if (cncount == 1) {
destlen = srclen - 1;
}
break;
}
if (!run) {
run = true;
for (int k = 0; k < start; k++) {
if (bb[k] < 0)
cn++;
}
if (cn % 2 > 0) {
i -= 2;
destb = new byte[++srclen];
continue;
}
} else {
run = true;
}
if (cncount == 2)
cncount = 0;
destb[j++] = bb[i];
if (bb[i] < 0) {
cncount++;
}
}
byte[] b = null;
if (destlen > 0) {
b = new byte[destlen];
System.arraycopy(destb, 0, b, 0, destlen);
s = new String(b);
} else {
s = new String(destb);
}
return s;
} public static void main(String[] args) { CreateTextImg cti = new CreateTextImg();
try {
BufferedImage image = cti.createImgByBg("测试qwerta试\n测试测试",
"footer", 100, 100);
// BufferedImage image =
// cti.createImgByBg("22222222222222222222222222", "footer", 500,
// 100);
cti.saveImg("F:/image.png", image,0.9f);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Image img = kit.getImage("icon.gif");