当前位置:网站首页>Copy the picture
Copy the picture
2020-11-08 21:03:00 【8Years】
public class TestBytesInputStream {
public static void main(String[] args) {
// Copy a.jpg picture
byte[] datas=fileToBytesArray("D:\\a.jpg");
byteArrayToFile(datas,"D:\\b.jpg");
}
//1. The image is read into the byte array
// 1. Picture to program ——FileInputStream
// 2. Program to byte array ——byteArrayOutputStream
public static byte[] fileToBytesArray(String path) {
// Create source and destination
File src = new File(path);
byte[] dest = null;
// Select flow
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// You can also use is = new BufferedInputStream(FileInputStream(src));
is = new FileInputStream(src);
// You can also use baos = new BufferedOutputStream(teArrayOutputStream());
baos = new ByteArrayOutputStream();
//3. Segment read
byte[] flush = new byte[1024 * 10];// Buffer container
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);// Write to a byte array
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void byteArrayToFile(byte[]src,String path){
File dest=new File(path);
InputStream is=null;
OutputStream os=null;
try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] flush = new byte[5];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(os!=null){
os.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
It's worth reminding , When is.read() When no parameters are passed in , The copied image will not open , Because it can only read byte by byte , So it's better to pass in the array
Two places to improve performance (1. The use of buffer containers ( It's like a pickup truck ) 2. You can also use byte buffer streams ( It's like a big truck ))
版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
边栏推荐
- 为什么需要使用API管理平台
- Learn volatile, you change your mind, I see
- If the programming language as martial arts unique! C++ is Jiu Yin Jing. What about programmers?
- opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
- CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
- Dynamic query processing method of stored procedure
- getBytes之 LengthFieldBasedFrameDecoder服务端解析
- 【杂谈】JS相关的线程模型整理
- 线程池运用不当的一次线上事故
- 深拷贝
猜你喜欢
随机推荐
MYCAT build
为什么需要使用API管理平台
JVM真香系列:轻松理解class文件到虚拟机(下)
[cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note
Django之简易用户系统(3)
接口测试工具Eolinker进行post请求
Flink series (0) -- Preparation (basic stream processing)
如何将 PyTorch Lightning 模型部署到生产中
给大家介绍下,这是我的流程图软件 —— draw.io
API生命周期的5个阶段
数组初相识
Brief introduction of Integrated Architecture
Five design schemes of singleton mode
Newbe.ObjectVisitor Example 1
CMS垃圾收集器
npm install 无响应解决方案
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
Five factors to consider before choosing API management platform
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020