Image操作

读取指定路径图片

 BufferedImage image = ImageIO.read(new FileInputStream(imgPath));

写出网络图片

	//获得图片地址
  Url img = new  URL(url);
  //获得图片输入流
  InputStream in = img.openStream();
  //把输入流转为BufferedImage
  JPEGImageDecoder decoderFile = JPEGCodec.createJPEGDecoder(in);
  BufferedImage image = decoderFile.decodeAsBufferedImage();
  //写出
  String fileName = UUID.randomUUID().toString().replaceAll("-","") + ".png";
  FileOutputStream   fos   =   new   FileOutputStream("d:/44/" + fileName);
  ImageIO.write(bufferedImage,"png", fos);
  

将图片 转换成为 Byte【】 数组

//获取 转换流
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
//将图片写入到流中
ImageIO.write(bufferedImage, "png", baos);
byte[] bytes = baos.toByteArray();//转换成字节

将图片写出

 String fileName = UUID.randomUUID().toString().replaceAll("-","") + ".png";
 FileOutputStream   fos   =   new   FileOutputStream("d:/44/" + fileName);
 ImageIO.write(bufferedImage,"png", fos);

借助虹软 只写出人脸

ImageInfo imageInfo = ImageFactory.bufferedImage2ImageInfo(bufferedImage);
Rect rect = faceInfo.getRect();
int x = rect.getLeft() > 20 ? rect.getLeft() -20 : 0;
int y = rect.getTop() > 20 ? rect.getTop() -20 : 0;
int w= rect.getRight() < imageInfo.getWidth() -20 ? rect.getRight() - x +20 : imageInfo.getWidth() - x;
int h = rect.getBottom() < imageInfo.getHeight() -20 ? rect.getBottom() - y + 20
						: imageInfo.getHeight() - y;
bufferedImage = bufferedImage.getSubimage(x, y, w, h);
		try {
					String fileName = UUID.randomUUID().toString().replaceAll("-","") + ".png";
					FileOutputStream   fos   =   new   FileOutputStream("d:/44/" + fileName);
					ImageIO.write(bufferedImage,"png", fos);
					fos.close();
					log.info("图片写入成功");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  

上传视频 操作 需要设定 传输的大小 否则音视频过大传输不上

spring:
  mvc:
    view:
      suffix: .html
  servlet:
    multipart:
      max-file-size: 300MB
      max-request-size: 500MB

上传视频 音频 需要在前端设定属性

<form action="http://localhost:8081/abc" method="post" 
		  enctype="multipart/form-data">  //enctype="multipart/form-data"  这个属性必须设定  如果只是单纯文本文件 则不需要
		<input name="fileImage" type="file" />
		<input type="submit" value="提交"/>
	</form>

接受 可用 spring 工具API MultipartFile

String path = "d:/11";
	/**
	 * 文件上传 需要 流的操作
	 * MultipartFile  springMVC 提供的工具api  实现文件上传的简化  主要操作 文件上传等操作  快速的进行多媒体的io流的读写操作  文件的上传
	 * @param fileImage
	 * @return
	 */
	@RequestMapping("/file")
	@ResponseBody
	public String file(MultipartFile fileImage ) {
		//将上传的类型 利用图片API进行转换   转换不成功 则不是图片
		try {
			//bufferedImage 处理图片的Api  从提交的输入流中 读取
			BufferedImage read = ImageIO.read(fileImage.getInputStream());
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return "不是图片";
		}
		
		File file = new File(path); //转换成为对象 方便操作
		if(!file.exists()) { //判断路径是否存在
			file.mkdirs(); //如果没有 创建多级目录信息
		}
		
		//准备文件上传的全路径  路径 + 文件名称
		String  filename = fileImage.getOriginalFilename(); //借助api 快速获取 文件名称+后缀
		File realFile = new File(path +"/"+ filename);  
		//将信息 输出到文件
		try {
			//实现文件的上传
			fileImage.transferTo(realFile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "上传失败";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "上传失败";
		}
		return "上传成功";
		}

springBoot 设置 虚拟路径

package com.tj.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
	
	@Override
	  public void addResourceHandlers(ResourceHandlerRegistry registry) {
	 
	//文件磁盘图片url 映射
	//配置server虚拟路径,handler为前台访问的目录,locations为files相对应的本地路径
	//	registry.addResourceHandler("/image/**").addResourceLocations("file:///D:\\11\\");
	registry.addResourceHandler("/image/**").addResourceLocations("file:d:/11/");
	  }


}

前端页面

<body>
	<img alt="图片损坏" src="http://localhost:8081/image/3.jpg">
</body>
</html>

效果展示

在这里插入图片描述

原文件

在这里插入图片描述

不大的图片 可以直接base64加密保存 前端直接请求这个加密数据就可

<img src="data:image/jpg;base64,base64默认加密数据>

写出图片工具方法

public static void byteToFile(String filePath, byte[] bytes, String fileName) {
		File file = null;
		File dir = new File(filePath);
		if (!dir.exists() && !dir.isDirectory()) {//判断文件夹是否存在
			dir.mkdirs();
		}
		BufferedOutputStream bos = null;
		java.io.FileOutputStream fos = null;
		try {
			file = new File(filePath + fileName); //创建图片文件  fileName  图片名字  filePath  为图片存放文件夹
			fos = new java.io.FileOutputStream(file);  //创建写出流
			bos = new BufferedOutputStream(fos); 
			bos.write(bytes); // 写出图片  bytes 为图片信息
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 CameraImage 转换为 Flutter 中用于显示的图像,需要使用 ImageConverter 将其转换为 Uint8List,并使用 decodeImage 将其解码为 Image。以下是一个示例代码片段,展示了如何使用 decodeImage 操作 CameraImage: ```dart import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:camera/camera.dart'; import 'package:image/image.dart' as Img; class CameraPreviewWidget extends StatelessWidget { final CameraImage image; const CameraPreviewWidget({ Key key, @required this.image, }) : super(key: key); @override Widget build(BuildContext context) { final Image img = Image.memory( Uint8List.view( image.planes[0].bytes.buffer, image.planes[0].bytes.offsetInBytes, image.planes[0].bytes.length, ), ); return img; } Future<Img.Image> convertCameraImage(CameraImage image) async { final int width = image.width; final int height = image.height; final Uint8List bytes = _concatenatePlanes(image.planes); return Img.decodeImage(bytes); } Uint8List _concatenatePlanes(List<Plane> planes) { final Completer<List<Uint8List>> completer = Completer<List<Uint8List>>(); final List<Uint8List> bytesList = <Uint8List>[]; int bytes = 0; for (int i = 0; i < planes.length; i++) { bytes += planes[i].bytes.length; bytesList.add(planes[i].bytes); } final Uint8List concatenatedBytes = Uint8List(bytes); int index = 0; for (int i = 0; i < planes.length; i++) { concatenatedBytes.setRange(index, index + planes[i].bytes.length, planes[i].bytes); index += planes[i].bytes.length; } return concatenatedBytes; } } ``` 在这个示例中,我们首先使用一个 Uint8List.view 将 CameraImage 中的字节拼接在一起,然后使用 decodeImage 将其解码为 Image。注意,在上面的代码中,我们使用了一个名为 _concatenatePlanes 的辅助函数,将不同平面的字节拼接在一起。 希望这可以帮助你解决问题!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值