您的位置:首页 > 编程语言 > Java开发

Jcrop插件图像截取 + java图像处理(缩略并截取)+ 用户截取的图片内容识别为字符串后设置页面input值

2019-04-24 11:38 253 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/yh_852974543/article/details/89487617

项目需求:把网页上的图片根据用户截取内容识别字符串并返回到前端页面。

前端引入js和css

<script src="js/jquery.Jcrop.js"></script>
<link rel="stylesheet" href="css/jquery.Jcrop.css" media="all">

前台代码jcrop的使用

再需要截图的图片上添加 id="element_id"

[code]//使用插件截取 设置回调,获取坐标
$.Jcrop("#element_id",{
bgOpacity: .85, //截图时背景的透明度
onSelect: showPreview, //选中后的操作,获取坐标
});

//网页图片的地址
imagePath = $("#element_id").attr("src");

function showPreview(c) {
console.log(c.x); //得到选中区域左上角横坐标
console.log(c.y);//得到选中区域左上角纵坐标
console.log(c.w);//得到选中区域的宽度
console.log(c.h);//得到选中区域的高度
//提交数据到后台重新生成截取后的图片
$.ajax({
url: "/image/partImageOcr",
type: "post",
data: {imagePath: imagePath, x: c.x, y: c.y, width:c.w, height:c.h},
dataType:"json",
success: function (data) {
if(data.status){
//识别成功后放到指定的字段
var entityName = $(".formDiv").find("input[name='entityName']");
entityName.focus();//获取焦点
$("input:focus").val(data.data); //给当前光标字段设置识别后的字符串
} else {
layer.msg(data.msg,{icon:5});
}
},
error: function (e) {
alert("异常,请重试!");
}

})
}

后台代码controller

[code]/*
识别用户截图部分
*/
@RequestMapping(value="partImageOcr")
public ReturnDataEntity partImageOcr(String imagePath,int x,int y,int width,int height){
System.out.println("远程图片地址:" + imagePath);
System.out.println("坐标:" + x + "," + y + "," + width + "," + height);
//先将远程图片转换为base64的编码
ImageUtils image = new ImageUtils();
if(imagePath == null || imagePath.equals("")){
return new ReturnDataEntity(GlobalConstant.FALSE_FAILE, GlobalConstant.CODE_FAILE, "远程图片地址为空", null);
}
String result = image.ImageUrlToBase64(imagePath);
if(result == null || result.equals("") ){
return new ReturnDataEntity(GlobalConstant.FALSE_FAILE, GlobalConstant.CODE_FAILE, "远程图片转换base64异常", null);
}

//在将远程图片生成缩略图
String strImage= image.thumbnailImage(result, GlobalConstant.thubWidth, GlobalConstant.thubHeight);
if(strImage == null || strImage.equals("")){
return new ReturnDataEntity(GlobalConstant.FALSE_FAILE, GlobalConstant.CODE_FAILE, "远程图片生成缩略图异常", null);
}

//然后根据用户截图的坐标后台截取图片返回字节数组 并用百度通用识别接口识别
byte[] bytes = image.cutImage(strImage,x, y, width, height);
String str = AipOcrUtil.basicGeneralByte(bytes);
if(str == null || str.equals("")){
return new ReturnDataEntity(GlobalConstant.FALSE_FAILE, GlobalConstant.CODE_FAILE, "截图识别失败", null);
}

return new ReturnDataEntity(GlobalConstant.TRUE_SUCCESS, GlobalConstant.CODE_SUCCESS, "", str);
}

后台代码ImageUtils

[code]import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Base64;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import com.shebao.common.ocr.AipOcrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageUtils {

private Logger log = LoggerFactory.getLogger(ImageUtils.class);
/**
* <p>Title: cutImage</p>
* <p>Description:  根据原图与裁切size截取局部图片</p>
* @param strImage    源图片base64串
*/
public byte[] cutImage(String strImage, int x, int y, int width, int height ){
Rectangle rect = new java.awt.Rectangle(x, y, width, height);
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(strImage);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

if(strImage != null && !strImage.equals("")){
java.io.FileInputStream fis = null;
ImageInputStream iis = null;
try {
//fis = new FileInputStream(srcImg);
String suffix = "jpg";
// 将FileInputStream 转换为ImageInputStream
iis = ImageIO.createImageInputStream(inputStream);
// 根据图片类型获取该种类型的ImageReader
ImageReader reader = ImageIO.getImageReadersBySuffix(suffix).next();
reader.setInput(iis,true);
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ImageIO.write(bi, suffix, outStream);
byte[] byteImage = outStream.toByteArray();
outStream.close();
Base64.Encoder encoder = Base64.getEncoder();
String result = encoder.encodeToString(byteImage);
System.out.println("用户的截图区域base64编码==================" + result);
return byteImage;
/* Base64.Encoder encoder = Base64.getEncoder();
String result = encoder.encodeToString(byteImage);
return result;*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis != null) fis.close();
if(iis != null) iis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
log.warn("the src image is not exist.");
}
return null;
}

/**
* <p>Title: thumbnailImage</p>
* <p>Description: 根据图片路径生成缩略图 </p>
* @param w            缩略图宽
* @param h            缩略图高
*/
public String thumbnailImage(String imgStr, int w, int h){

Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(imgStr);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

if(imgStr != null && !imgStr.equals("")){
try {
String suffix = "jpg";
//Image img = ImageIO.read(srcImg);
BufferedImage img = ImageIO.read(inputStream);
// 根据指定长宽进行缩略
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
g.dispose();
inputStream.close();
// 将图片转换为base64字符串
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ImageIO.write(bi, suffix, outStream);
Base64.Encoder encoder = Base64.getEncoder();
String result = encoder.encodeToString(outStream.toByteArray());
outStream.close();
return result;
} catch (IOException e) {
log.error("generate thumbnail image failed.",e);
}
} else {
log.warn("the src image is not exist.");
}
return null;
}

/**
* 远程读取image转换为Base64字符串
* @param imgUrl
* @return
*/
public  String ImageUrlToBase64(String imgUrl) {
InputStream is = null;
ByteArrayOutputStream outStream = null;
HttpURLConnection conn = null;
try{
URL url = new URL(imgUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
conn.getInputStream();
is = conn.getInputStream();
byte[] data = readInputStream(is);
// 对字节数组Base64编码
Base64.Encoder encoder = Base64.getEncoder();
String result = encoder.encodeToString(data);
//result = "data:image/png;base64," + result;
return result;

}catch (Exception e) {
e.printStackTrace();
}
finally{
if(is != null)
{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outStream != null)
{
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(conn != null)
{
conn.disconnect();
}
}
return null;
}

/**
* 读取图片流
* @param inStream
* @return
* @throws IOException
*/
private  byte[] readInputStream(InputStream inStream) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=inStream.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}

public static void main(String[] args) {
ImageUtils image = new ImageUtils();
        String imagePath = "";
String result = image.ImageUrlToBase64(imagePath);

//生成的缩略图
String strImage= image.thumbnailImage(result, 2000, 1300);
byte[] bytes = image.cutImage(strImage,500, 300, 300, 300);

//百度识别通用接口
String str = AipOcrUtil.basicGeneralByte(bytes);
System.out.println("截图识别字符串===============" + str);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: