java导出图片
1. java编写时,怎么输出一张图片呀
(1)首先用ImageIO类读取这张图片
(2)如果要对这张图片修改,通过图片获取Graphics对象,再调用Graphics的方法来绘制、修改。
(3)再调用ImageIO的方法将图片输出到特定IO流即可。
具体代码实例可参考李刚的疯狂Java讲义的11.8节。
2. JAVA图片输出
等着拿分.......
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* @author alanwei
*
*/
public class Test {
public static BufferedImage createImage(int width, int height, String s) {
Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(s, (int)x, (int)baseY);
return bi;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedImage image = createImage(100, 20, "123456789");
File file = new File("image.jpg");
if (!file.exists()) {
file.createNewFile();
}
if (image != null) {
ImageIO.write(image, "jpg", file);
}
}
}
3. 怎样用JAVA编写一个小应用程序,输出一张图片或自己的照片
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class text
{
JFrame jf;
JLabel jl;
JPanel jp1;
public static void main(String[] args)
{
text t=new text();
t.go();
}
void go()
{
final ImageIcon image1 = new ImageIcon("1.gif");
jf=new JFrame();
jl = new JLabel(image1);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 300);
jf.setLocation(400, 300);
jp1 = new JPanel();
jp1.add(jl);
jf.getContentPane().add(jp1);
jf.setVisible(true);
}
}
把你的找一张gif 格式的照片 .名字改成1.gif 放在你的项目根目录下
4. JAVA中如何将Graphics g绘制的图像导出成BMP或者JPG文件
如果要导出图片文件,需要先单独创建出一个BufferedImage对象,通过getGraphices()取得对象,绘制好图象后再通过ImageIO的write方法导出成图片文件。
5. java如何导出多个图片到excel
给你个方法,你新建一个EXECL在不同的sheet中随便写点什么东西,然后另存为*.html文件,用浏览器打开,查看源文件,照着上面改一下,就可以了。
6. 请教java导出多张图片到Excel问题!
package tei;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcelImage {
static List<BufferedImage> images = new ArrayList<>();
static {
try {
images.add(ImageIO.read(new File("C:/t/1.jpg")));
images.add(ImageIO.read(new File("C:/t/2.jpg")));
images.add(ImageIO.read(new File("C:/t/3.jpg")));
images.add(ImageIO.read(new File("C:/t/4.jpg")));
images.add(ImageIO.read(new File("C:/t/5.jpg")));
images.add(ImageIO.read(new File("C:/t/6.jpg")));
images.add(ImageIO.read(new File("C:/t/7.jpg")));
images.add(ImageIO.read(new File("C:/t/8.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileOutputStream fileOut = null;
try {
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
// HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
short i = 0;
for (BufferedImage image : images) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1 + i, (short) 2, 2 + i);
anchor.setAnchorType(0);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
i++;
}
HSSFRow row = sheet1.createRow(10);
short s = 10;
HSSFCell cell = row.createCell(s);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setStrikeout(true);
style.setFont(font);
cell.setCellStyle(style);
cell.setCellValue("aaaaa");
fileOut = new FileOutputStream("c:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();
} catch (IOException io) {
io.printStackTrace();
System.out.println("io erorr : " + io.getMessage());
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
简单的写了下。 我机器上可以运行
7. java中输出图片的代码
final ImageView iv=(ImageView)findViewById(R.id.iv);
Button bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
// TODO: Implement this method
if(iv.getDrawable()!=null)
iv.setImageResource(R.id.photo);
else iv.setImageResource(0);
}
});
8. java怎么在控制台输出一张jpg的图片
输出图片的base64编码
//imgFile是图片的路径
publicstaticvoidgetImageStr(StringimgFile){
InputStreaminputStream=null;
byte[]data=null;
try{
inputStream=newFileInputStream(imgFile);
data=newbyte[inputStream.available()];
inputStream.read(data);
inputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}//加密
BASE64Encoderencoder=newBASE64Encoder();
System.out.println(encoder.encode(data));
}
9. 请问 用java语句输出如图片图案应该怎么做
1、代码如下:
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World!");
//主循环
for(int i =10;i>0;i--){
//输出空格
for(int k=i;k>0;k--){System.out.print(" ");}
//输出数字
for(int j=i;j<=10;j++){
System.out.print(j+" ");
}System.out.println(" ");
}
}
}
2、效果如图
10. 请问下java中导出图片怎么做
package com.xolt;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
ImageIO.write(bufferImg1,"jpg",byteArrayOut1);
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("poi picT");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);
anchor1.setAnchorType(2);
//插入图片
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("c:/workbook.xls");
//写入excel文件
wb.write(fileOut);
fileOut.close();
}catch(IOException io){
io.printStackTrace();
System.out.println("io erorr : "+ io.getMessage());
} finally
{
if (fileOut != null)
{
try {
fileOut.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
poi中图片到到excel的方法 你需要准备poi包 试试看看