「图片拷贝java」图片拷贝后都无法显示
今天给各位分享图片拷贝java的知识,其中也会对图片拷贝后都无法显示进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、在Java中,怎样将图片从一个地方复制到另一个地方?(最好有代码)
- 2、用Java编写一个程序,将一个图像文件复制到指定的文件夹中
- 3、java 编程,复制图片到另一文件夹下,如何提高效率
- 4、java怎样复制图片
- 5、一个关于JAVA拷贝图片的问题
在Java中,怎样将图片从一个地方复制到另一个地方?(最好有代码)
就在电话里就可以调啊!找到那图片~点!有个移动!还是复制的!点完!在点你要移的地方!就OK了!如果你不习惯!你电脑上也可以啊!查数据线
用Java编写一个程序,将一个图像文件复制到指定的文件夹中
这是我们公司基类里的一个方法希望对你有帮助。。/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
// System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace(); } }
java 编程,复制图片到另一文件夹下,如何提高效率
直接用文件流打开一个文件,在通过楼下说的缓冲流将文件直接写到另外一个文件就可以了
//处理JPEG的
public static String getFixedBoundIcon(String filePath) throws Exception {
//返回地址
String result = "";
//输出流
FileOutputStream out = null;
try {
File f = new File(filePath);
if (!f.isFile()) {
throw new Exception(f + " 不是图片文件!");
}
//图象文件
if (f != null f.exists()) {
//这里的ImageIO属于java工厂类,在工厂类class里面,调用的System.gc(),频繁调用会造成dump,需要考虑优化
BufferedImage image = ImageIO.read(f); // 读入文件
if (image != null) {
BufferedImage tag =
new BufferedImage(116, 165, BufferedImage.TYPE_INT_RGB);
//绘制缩小后的图
tag.getGraphics().drawImage(image, 0, 0, 116, 165, null);
//文件地址部分
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0, lastLength);
String fileType = filePath.substring(lastLength);
//背景
result = subFilePath + "_116_165" + fileType;
out = new FileOutputStream(result);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(tag);
param.setQuality(0.95f, true); //95%图像
//像素尺寸单位.像素/英寸
param.setDensityUnit(1);
//水平分辨率
param.setXDensity(300);
//垂直分辨率
param.setYDensity(300);
encoder.setJPEGEncodeParam(param);
encoder.encode(tag);
tag = null;
}
}
f = null;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
out.close();
out = null;
}
return result;
}
还要try起来捕获异常哟
java怎样复制图片
图片本质上还是文件,所以就像复制文件一样就可以了。下面是一个演示程序:
public class CopyImage
{
public static void main(String[] args) throws Exception
{
FileInputStream fi=new FileInputStream("image.jpg");
BufferedInputStream in=new BufferedInputStream(fi);
FileOutputStream fo=new FileOutputStream("cimage.jpg");
BufferedOutputStream out=new BufferedOutputStream(fo);
byte[] buf=new byte[4096];
int len=in.read(buf);
while(len!=-1)
{
out.write(buf, 0, len);
len=in.read(buf);
}
out.close();
fo.close();
in.close();
fi.close();
}
}
运行程序是改一改图片的路径,另外在实际代码中最后不要想上面的代码直接抛出这样的异常。
一个关于JAVA拷贝图片的问题
import java.io.*;
public class BisicImageCopy {
public static void main(String[] args) {
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream("e:\\Car.jpg");
fout = new FileOutputStream("d:\\Car.jpg");
byte[] b = new byte[512];
int n;
while((n = fin.read(b))!=-1){
fout.write(b,0,n);//这里改改
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fin.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
关于图片拷贝java和图片拷贝后都无法显示的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。