java 保存图片
ImageIO.write(BufferedImage, "JPG", File);
在淮南等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、网站制作 网站设计制作专业公司,公司网站建设,企业网站建设,品牌网站建设,成都全网营销推广,外贸营销网站建设,淮南网站建设费用合理。
================================
传入Component保存图像的方法,你试试看还有没有变色。
public void cutScreen(Component com) {
Rectangle rect = com.getBounds();
BufferedImage bi = (BufferedImage) com.createImage(rect.width,
rect.height);
Graphics g = bi.getGraphics();
com.paint(g);
g.dispose();
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory()
|| f.getName().toLowerCase().endsWith(".jpg");
}
public String getDescription() {
return "*.jpg";
}
});
int type = jfc.showSaveDialog(null);
if (type == 0) {
File file = jfc.getSelectedFile();
name = file.getName().toLowerCase();
if (!name.endsWith("jpg")) {
String path = file.getAbsolutePath();
file = new File(path + ".jpg");
for (int i = 0; file.exists(); i++) {
file = new File(path + "(" + i + ").jpg");
}
}
try {
if (!file.exists()) {
file.createNewFile();
}
ImageIO.write(bi, "JPG", file);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
求java存图片绝对路径到mysql的代码
首先,建立数据库(主意image的数据类型):
CREATE TABLE image (
id int(5) NOT NULL,
name varchar(25) default NULL,
photo blob,
PRIMARY KEY (`id`)
);
以下是Java Code:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root");
String INSERT_PICTURE = "insert into image (id, name, photo) values (?, ?, ?)";
FileInputStream fis = null;
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
File file = new File("myPhoto.png");
fis = new FileInputStream(file);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setString(1, "001");
ps.setString(2, "name");
ps.setBinaryStream(3, fis, (int) file.length());
ps.executeUpdate();
conn.commit();
} finally {
ps.close();
fis.close();
}
}
Java Graphics绘图完成后,然后怎么保存成图片的问题
使用BufferedImage、创建Graphics、然后保存这个BufferedImage。。。。。。。。
java 实现截图并且 保存在本地
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.filechooser.FileSystemView;
/**
* java截屏
* 运行后将当前屏幕截取,并最大化显示。
* 拖拽鼠标,选择自己需要的部分。
* 按Esc键保存图片到桌面,并退出程序。
* 点击右上角(没有可见的按钮),退出程序,不保存图片。
*
* @author JinCeon
*/
public class SnapshotTest {
public static void main(String[] args) {
// 全屏运行
RectD rd = new RectD();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
gd.setFullScreenWindow(rd);
}
}
class RectD extends JFrame {
private static final long serialVersionUID = 1L;
int orgx, orgy, endx, endy;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image;
BufferedImage tempImage;
BufferedImage saveImage;
Graphics g;
@Override
public void paint(Graphics g) {
RescaleOp ro = new RescaleOp(0.8f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
}
public RectD() {
snapshot();
setVisible(true);
// setSize(d);//最大化窗口
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orgx = e.getX();
orgy = e.getY();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endx = e.getX();
endy = e.getY();
g = getGraphics();
g.drawImage(tempImage, 0, 0, RectD.this);
int x = Math.min(orgx, endx);
int y = Math.min(orgy, endy);
int width = Math.abs(endx - orgx)+1;
int height = Math.abs(endy - orgy)+1;
// 加上1,防止width或height为0
g.setColor(Color.BLUE);
g.drawRect(x-1, y-1, width+1, height+1);
//减1,加1都是为了防止图片将矩形框覆盖掉
saveImage = image.getSubimage(x, y, width, height);
g.drawImage(saveImage, x, y, RectD.this);
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// 按Esc键退出
if (e.getKeyCode() == 27) {
saveToFile();
System.exit(0);
}
}
});
}
public void saveToFile() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
String name = sdf.format(new Date());
File path = FileSystemView.getFileSystemView().getHomeDirectory();
String format = "jpg";
File f = new File(path + File.separator + name + "." + format);
try {
ImageIO.write(saveImage, format, f);
} catch (IOException e) {
e.printStackTrace();
}
}
public void snapshot() {
try {
Robot robot = new Robot();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
image = robot.createScreenCapture(new Rectangle(0, 0, d.width,
d.height));
} catch (AWTException e) {
e.printStackTrace();
}
}
}
从文件中读取图片和写入图片到文件里的java代码是什么?
首先导入各种需要的包:
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
读取图片的方法如下:
Image[] array = new Image[10];
Image image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧
array[0] = image;
图片读出来了。
如果你有一个Image对象,想把它写入文件可以这样做:
BufferedImage image = ImageIO.read(new File("d:\\source.gif"));
//要想保存这个对象的话你要把image声明为BufferedImage 类型
ImageIO.write(image, "png", new File("f:\\test.png"));
新闻名称:java保存图片的代码 java如何处理图片
链接地址:http://lswzjz.com/article/hgdhho.html