Commit 8b9a1faf by zhouxiao

添加指纹图片处理的方法,使其可以成功入库

parent c8108f54
package com.common;
import net.coobird.thumbnailator.Thumbnails;
import net.sf.json.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.*;
import java.lang.reflect.Field;
import java.text.DateFormat;
......@@ -47,20 +37,6 @@ public class buildFingerUtil {
out.close();
}
/**
* 将base64字符解码然后Thumbnails图片压缩
* @author zhouxiao
*/
public static String compressPic(String filepath) throws Exception {
ByteArrayOutputStream outstream = null;
byte[] buffer = new byte[1024];
outstream = new ByteArrayOutputStream(buffer.length);
Thumbnails.of(filepath).scale(0.5f).toOutputStream(outstream);
buffer = outstream.toByteArray();
//要去除空格
return new BASE64Encoder().encode(buffer).replaceAll("\\s*|\t|\r|\n","");
}
/**
* 生成通用Element节点
* @param el 根节点的上一个节点
* @param obj 节点属性名
......@@ -161,50 +137,6 @@ public class buildFingerUtil {
return listname;
}
/**
* 将文件读出来并通过base64加密,然后旋转图片
* @param file 根据文件路径new一个file对象
* @return
* @author zhouxiao
* @throws Exception
* size(width,height) 图片长宽
* rotate(角度),正数:顺时针 负数:逆时针
* scale(比例)
* keepAspectRatio(false) 默认是按照比例缩放的
* watermark(位置,水印图,透明度)
* outputFormat(图像格式)
* date:20210415之前这种方法压缩图片并存储xml打开报错
*/
public static String compressPic(File file) throws Exception {
ByteArrayInputStream in = null;
ByteArrayOutputStream outstream = null;
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((bytesRead = inputFile.read(buffer)) != -1) {
bao.write(buffer, 0, bytesRead);
}
inputFile.close();
byte[] data = bao.toByteArray();
/*inputFile.read(buffer);
inputFile.close();*/
//System.out.println("以上是原始数据");
//System.out.println("下面是压缩后的数据");
in = new ByteArrayInputStream(data);
outstream = new ByteArrayOutputStream(buffer.length);
Thumbnails.of(in).scale(0.4f).rotate(180).toOutputStream(outstream);
//Thumbnails.of(in).size(640,640).keepAspectRatio(false).toOutputStream(outstream);
byte[] baoffer = outstream.toByteArray();
//String bufstr = new BASE64Encoder().encode(baoffer).replaceAll("\\s*|\t|\r|\n","");
//要去除空格
return new BASE64Encoder().encode(baoffer).replaceAll("\\s*|\t|\r|\n","");
}
/**
* 将文件读出来并通过base64加密
* @param file 根据文件路径new一个file对象
* @author zhouxiao
......@@ -235,74 +167,44 @@ public class buildFingerUtil {
writer.flush();
writer.close();
}
/**
* 原样读取图片的二进制字节流
* @param file
* @return
* @author zhouxiao
* @throws Exception
* 目前没用到
*/
public static byte[] readInputStream(File file) throws Exception {
//File file = new File(path);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
int len = 0;
while ((len = inputFile.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
inputFile.close();
return outputStream.toByteArray();
}
/**
* 该方法的目的是解析xml文件
* 将xml字符串转换为Document对象
* @author zhouxiao
* @param xmlStr
* @return
*/
public static Document xmlStr2Document(String xmlStr){
Document document = null;
try{
document = DocumentHelper.parseText(xmlStr);
}catch(Exception e){
e.printStackTrace();
}
return document;
}
public static void image2RGB565Bmp(String filePath, String saveFileName) {
try {
BufferedImage sourceImg = ImageIO.read(new File(filePath));
int h = sourceImg.getHeight(), w = sourceImg.getWidth();
int[] pixel = new int[w * h];
PixelGrabber pixelGrabber = new PixelGrabber(sourceImg, 0, 0, w, h, pixel, 0, w);
pixelGrabber.grabPixels();
MemoryImageSource m = new MemoryImageSource(w, h, pixel, 0, w);
Image image = Toolkit.getDefaultToolkit().createImage(m);
BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_USHORT_565_RGB);
buff.createGraphics().drawImage(image, 0, 0 ,null);
ImageIO.write(buff, "bmp", new File(saveFileName));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 去掉图片的base64的头部
* @param baseurl
* @return
* @throws Exception
* 指纹图片入库-出库流程:
* 入库:
* 将8bit bmp文件转换为1维字节数组,这个数组包含:位图文件头,位图信息头,位图颜色表,位图数据四部分,
* 从数组的1078位置(位图数据从1078开始)开始截取,得到位图数据;
* 从数组的22-26位截取,得到bmp源文件的高;
* 从数组的18-22位截取,得到bmp源文件的宽;
* 然后结合位图数据,源文件的宽,高,将1维数组转换成2维数组并置换排序(Windows扫描默认从低位到高位);
* 将处理完的2维数组转成1维数组;
* 将1维数组转成Base64字符串,并格式化(注意:必须去掉空格,换行符);
* 最后将Base64字符串入库。
*
* 出库:
* 将入库的Base64字符串转成1维数组(位图数据);
* 得到指纹图片的宽高(源数据的宽高是640*640),设置位图文件头,位图信息头,位图颜色表;
* 将位图数据和上面的位图信息合并生成完整的bmp字节数组;
* 最后将字节数组转换成Bmp文件进行显示。
*/
public static String baseurlPhotos(String baseurl) {
return baseurl.substring(baseurl.indexOf(",") + 1);
//return baseurl.substring(1078,baseurl.length());
//入库:需要的base64图片去头,按照从上到下扫描顺序
public static String Sbase2Tbase64str(String filename){
//1、将文件转换成Byte数组
byte[] ioByte = ToolUtil.getBytesByFile(filename);
//2、处理base64图片byte数组,去头,翻转
byte[] dealData = ToolUtil.dealBmpByByte(ioByte);
//3、2的字节数组转base64字符串
String base64Str = ToolUtil.byteArrayToBase64String(dealData);
assert base64Str != null;
//4、将3的字符串去空格换行
String formatBase64Str = ToolUtil.formatBase64String(base64Str);
return formatBase64Str;
}
//出库:将base64图片加头还原成原图
public static void Tbase2Sbase64str(String formatBase64Str,String filePath,String fileName) throws IOException{
//1、base64字符串转字节数组
byte[] imageBytes = ToolUtil.base64StringToByteArray(formatBase64Str);
//2、将去头后的base64字符串还原,转换成字节数组
byte[] fingerprintBmpByte = ToolUtil.addHeadImage(imageBytes,640,640);
//3、将Byte数组转换成文件
ToolUtil.getFileByBytes(fingerprintBmpByte,filePath,fileName);
}
}
......@@ -16,7 +16,7 @@ import java.io.StringWriter;
import java.util.*;
/**
* Created by founder on 2021/7/2.
* Created by zhouxiao on 2021/7/2.
*/
@Service
public class fingerImgService {
......@@ -85,23 +85,12 @@ public class fingerImgService {
Map<String,String> map = new HashMap<>();
//读取每个图片的二进制流并通过base64加密
File readfile = new File(basePath+"\\"+rybh + "\\" + filelist[i]);
String filname = basePath+"\\"+rybh + "\\" + filelist[i];
try {
//byte[] imgStr = buildFingerUtil.readInputStream(readfile);//原始图片流
String imgStr1 = buildFingerUtil.encodeBase64File(readfile);//解密后的二进制bs64流
//String imgStr1 = buildFingerUtil.compressPic(basePath+"\\"+rybh + "\\" + filelist[i]);//先解压,然后压缩的二进制bs64流
//String imgStr1 = buildFingerUtil.compressPic(readfile);//压缩后的二进制bs64流
//String imgStr = buildFingerUtil.baseurlPhotos(imgStr1);
System.out.println("imgStr1==="+imgStr1);
/*String target = "F:/test/xzzw/eww"+i+".bmp";
buildFingerUtil.decoderBase64File(imgStr,target);
File xsfile = new File(target);
buildFingerUtil.xs(xsfile);*/
String imgStr = buildFingerUtil.Sbase2Tbase64str(filname);//解密后的二进制bs64流
System.out.println("imgStr==="+imgStr);
//处理照片
map.put(readfile.getPath(), imgStr1);
map.put(readfile.getPath(), imgStr);
list.add(map);
}catch(Exception e){
System.out.println("生成文件名和图片流对应关系异常"+e);
......
......@@ -6,7 +6,7 @@ import java.io.IOException;
import java.util.Properties;
/**
* Created by founder on 2021/6/28.
* Created by zhouxiao on 2021/6/28.
*/
@Service
public class fingerInfoAnalyService {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment