package base.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 照片操作的工具类
* @author ZhengLing
*
*/
public class PictureUtil {
private static Logger logger = LoggerFactory.getLogger(PictureUtil.class);
/**
* 取得身份照片
* @param certID
* 身份证ID
* @param picType
* 照片类型(身份证:0; 大头照:1; 指纹:3)
* @return 照片字节
*/
public static byte[] getCertificatePic(String certID, int picType){
String path = PropertiesTools.getConfigProperties("picturePath") + "certPic" + File.separator;
String areaCode = "";
String year = "";
if (certID.length() == 18) {
areaCode = certID.substring(0, 6);
year = certID.substring(6, 10);
}
if (certID.length() == 15) {
areaCode = certID.substring(0, 6);
year = "19" + certID.substring(8, 10);
}
path = path + areaCode + File.separator + year + File.separator;
logger.info("pictureManage路径为:"+path);
File file = new File(path + certID + "_" + picType + ".jpg");
byte[] c = null;
String secondPath = "";
File secondFile = null;
if (StringUtils.isNotEmpty(PropertiesTools.getConfigProperties("cert.pic.extend.folder"))) {
secondPath = PropertiesTools.getConfigProperties("picturePath")
+ PropertiesTools.getConfigProperties("cert.pic.extend.folder") + File.separator + areaCode
+ File.separator + year + File.separator;
secondFile = new File(secondPath + certID + "_" + picType + ".jpg");
}
try {
if (file.exists()) {
logger.info("get certificate first picture");
c = getBytesFromFile(file);
} else if (secondFile != null && secondFile.exists()) {
logger.info("get certificate second picture");
c = getBytesFromFile(file);
} else {
c = getDefalutPic();
}
} catch (Exception e) {
e.printStackTrace();
return getDefalutPic();
}
return c;
}
/**
* 取得控制台和客户机照片(picType为照片类型) 控制台:5; 客户机:6
* @param date 时间
* @param serviceCode 场所代码
* @param certID 身份
* @param picType
* @return
*/
public static byte[] getConsolePic(String date, String serviceCode, String certID, int picType){
String path = PropertiesTools.getConfigProperties("picturePath") + date + File.separator + serviceCode
+ File.separator + certID + File.separator;
File file = new File(path);
String[] fileNames = null;
if (file.isDirectory()) {
fileNames = file.list();
}
List fileNamesTmp = new ArrayList();
if (fileNames != null && fileNames.length > 0) {
for (int i = 0; i < fileNames.length; i++) {
if (fileNames[i].endsWith("_" + picType + ".jpg")) {
fileNamesTmp.add(fileNames[i]);
}
}
}
String fileName = "";
if (fileNamesTmp.size() < 1) {
// 如果是晚上凌晨的照片没有,就找最近2天的照片有没有
String tmpDate = DateUtil.dateStr2DateFomatStr(date, DateUtil.FORMAT_DATE_LUCENE, DateUtil.FORMAT_DATE);
long tm = DateUtil.dateStr2Long(DateUtil.FORMAT_DATE, tmpDate);
Calendar prevDate = Calendar.getInstance();
Calendar nextDate = Calendar.getInstance();
prevDate.setTimeInMillis(tm);
nextDate.setTimeInMillis(tm);
prevDate.set(Calendar.DATE, prevDate.get(Calendar.DATE) - 1);
nextDate.set(Calendar.DATE, nextDate.get(Calendar.DATE) + 1);
String prevDateStr =DateUtil.formatDate(prevDate.getTime(), "yyyyMMdd");
String nextDateStr = DateUtil.formatDate(nextDate.getTime(),"yyyyMMdd");
String prevDatePath = PropertiesTools.getConfigProperties("picturePath") + prevDateStr + File.separator
+ serviceCode + File.separator + certID + File.separator;
File prevDatefile = new File(prevDatePath);
String[] prevFileNames = null;
if (prevDatefile.isDirectory()) {
prevFileNames = prevDatefile.list();
}
String nextDatePath = PropertiesTools.getConfigProperties("picturePath") + nextDateStr + File.separator
+ serviceCode + File.separator + certID + File.separator;
List prevDatefileNamesTmp = new ArrayList();
if (prevFileNames != null && prevFileNames.length > 0) {
for (int i = 0; i < prevFileNames.length; i++) {
if (prevFileNames[i].endsWith("_" + picType + ".jpg")) {
prevDatefileNamesTmp.add(prevFileNames[i]);
}
}
}
if (prevDatefileNamesTmp.size() > 0) {
fileNamesTmp = prevDatefileNamesTmp;
} else {
File nextDatefile = new File(nextDatePath);
String[] nextFileNames = null;
if (nextDatefile.isDirectory()) {
nextFileNames = nextDatefile.list();
}
List nextDatefileNamesTmp = new ArrayList();
if (nextFileNames != null && nextFileNames.length > 0) {
for (int i = 0; i < nextFileNames.length; i++) {
if (nextFileNames[i].endsWith("_" + picType + ".jpg")) {
nextDatefileNamesTmp.add(nextFileNames[i]);
}
}
}
if (nextDatefileNamesTmp.size() > 0) {
fileNamesTmp = nextDatefileNamesTmp;
}
}
}
if (fileNamesTmp.size() > 0) {
fileName = fileNamesTmp.get(0).toString();
}
logger.info("--------consolpicpath:"+path + fileName);
File picFile = new File(path + fileName);
try {
if (picFile.exists() && !"".equals(fileName)) {
byte[] buffer1 = getBytesFromFile(picFile);
if (picType == 5) {
return buffer1;
} else if (picType == 6) {
byte[] buffer2 = Base64.decode(buffer1, 0, buffer1.length);
for (int i = 0; i < buffer2.length; ++i) {
byte b = buffer2[i];
buffer2[i] = (byte) (((b & 0xf) << 4) | ((b & 0xf0) >> 4));
}
return buffer2;
}
} else {
return getDefalutPic();
}
} catch (Exception e) {
e.printStackTrace();
return getDefalutPic();
}
return getDefalutPic();
}
/**
* 获取摄像头拍照的照片
* @return
*/
public static byte[] getClientPic() {
File file = new File(PropertiesTools.getConfigProperties("intercept_client_pic_path"));
logger.error("get client file = " + file.getAbsolutePath());
int count = 0;
byte[] buffer = getDefalutPic();
while (true) {
if (file.exists() && file.canRead()) {
try {
byte[] buffer1 = getBytesFromFile(file);
byte[] buffer2 = Base64.decode(buffer1, 0, buffer1.length);
for (int j = 0; j < buffer2.length; ++j) {
byte b = buffer2[j];
buffer2[j] = (byte) (((b & 0xf) << 4) | ((b & 0xf0) >> 4));
}
return buffer2;
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 超过15秒未取到文件则判断为客户机拍照失败
if (count > 15) {
break;
} else {
try {
Thread.sleep(1000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
return buffer;
}
/**
* 获取实时监控截屏的照片
* @return
*/
public static File getRealTimeScreen() {
int count=0;
File file = new File(PropertiesTools.getConfigProperties("intercept_pic_path"));
while (true) {
/*String path="";
try {
if(file!=null){
path=file.getParent()+File.separator;
CommandRunner commandRunner = new CommandRunner();
commandRunner.runLocal("ls " + path);
//commandRunner.run("ls " + path);
logger.info("显示截屏照片,刷新目录,path="+path);
}
} catch (Exception e) {
logger.error("显示截屏照片,刷新目录时出现错误,path="+path,e);
}*/
if (file!=null&&file.exists() && file.canRead()) {
logger.info("显示截屏照片,已经找到照片!!path="+file.getAbsolutePath());
break;
} else {
// 超过15秒未取到文件则判断为截屏失败
if (count > 15) {
logger.info("显示截屏照片,等待次数超过15秒,没有获取到照片!!");
break;
} else {
try {
logger.info("显示截屏照片,等待次数count="+count+",exists="+file.exists()+",canRead="+file.canRead());
Thread.sleep(1000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
return file;
}
/**
* 取得控制台和客户机照片(picType为照片类型) 控制台:5 客户机:6
*
* @param date
* @param serviceCode
* @param certID
* @param picType
* @return
*/
public static byte[] getConsolePicBySessionId(String date, String serviceCode, String certID, String sessionId, int picType) {
String path = PropertiesTools.getConfigProperties("picturePath") + date + File.separator + serviceCode
+ File.separator + certID + File.separator;
logger.info("path=" + path);
File file = new File(path);
String[] fileNames = null;
if (file.isDirectory()) {
fileNames = file.list();
}
List fileNamesTmp = new ArrayList();
if (fileNames != null && fileNames.length > 0) {
long lastFileTime = 0;
for (int i = 0; i < fileNames.length; i++) {
if (StringUtils.isNotEmpty(sessionId)) {
if (fileNames[i].equals(sessionId + "_" + picType + ".jpg")) {
fileNamesTmp.add(path + fileNames[i]);
}
} else {
if (fileNames[i].endsWith("_" + picType + ".jpg")) {
File lastTimeFile = new File(path + fileNames[i]);
if (lastTimeFile.exists()) {
if (lastTimeFile.lastModified() > lastFileTime) {
fileNamesTmp.add(path + fileNames[i]);
lastFileTime = lastTimeFile.lastModified();
System.out.println("fileNames=" + fileNames[i]);
}
}
}
}
}
}
String fileName = "";
if (fileNamesTmp.size() < 1) {
// 如果是晚上凌晨的照片没有,就找最近2天的照片有没有
String tmpDate = DateUtil.dateStr2DateFomatStr(date, DateUtil.FORMAT_DATE_LUCENE, DateUtil.FORMAT_DATE);
long tm = DateUtil.dateStr2Long(DateUtil.FORMAT_DATE, tmpDate);
Calendar prevDate = Calendar.getInstance();
Calendar nextDate = Calendar.getInstance();
prevDate.setTimeInMillis(tm);
nextDate.setTimeInMillis(tm);
prevDate.set(Calendar.DATE, prevDate.get(Calendar.DATE) - 1);
nextDate.set(Calendar.DATE, nextDate.get(Calendar.DATE) + 1);
String prevDateStr =DateUtil.formatDate(prevDate.getTime(), "yyyyMMdd");
String nextDateStr = DateUtil.formatDate(nextDate.getTime(),"yyyyMMdd");
String prevDatePath = PropertiesTools.getConfigProperties("picturePath") + prevDateStr + File.separator
+ serviceCode + File.separator + certID + File.separator;
File prevDatefile = new File(prevDatePath);
String[] prevFileNames = null;
if (prevDatefile.isDirectory()) {
prevFileNames = prevDatefile.list();
}
List prevDatefileNamesTmp = new ArrayList();
if (prevFileNames != null && prevFileNames.length > 0) {
for (int i = 0; i < prevFileNames.length; i++) {
if (StringUtils.isNotEmpty(sessionId)) {
if (prevFileNames[i].equals(sessionId + "_" + picType + ".jpg")) {
prevDatefileNamesTmp.add(prevDatePath + prevFileNames[i]);
}
} else {
if (prevFileNames[i].endsWith("_" + picType + ".jpg")) {
prevDatefileNamesTmp.add(prevDatePath + prevFileNames[i]);
}
}
}
}
String nextDatePath = PropertiesTools.getConfigProperties("picturePath") + nextDateStr + File.separator
+ serviceCode + File.separator + certID + File.separator;
if (prevDatefileNamesTmp.size() > 0) {
fileNamesTmp = prevDatefileNamesTmp;
} else {
File nextDatefile = new File(nextDatePath);
String[] nextFileNames = null;
if (nextDatefile.isDirectory()) {
nextFileNames = nextDatefile.list();
}
List nextDatefileNamesTmp = new ArrayList();
if (nextFileNames != null && nextFileNames.length > 0) {
for (int i = 0; i < nextFileNames.length; i++) {
if (StringUtils.isNotEmpty(sessionId)) {
if (nextFileNames[i].equals(sessionId + "_" + picType + ".jpg")) {
nextDatefileNamesTmp.add(nextDatePath + nextFileNames[i]);
}
} else {
if (nextFileNames[i].endsWith("_" + picType + ".jpg")) {
nextDatefileNamesTmp.add(nextDatePath + nextFileNames[i]);
}
}
}
}
if (nextDatefileNamesTmp.size() > 0) {
fileNamesTmp = nextDatefileNamesTmp;
}
}
}
if (fileNamesTmp.size() > 0) {
fileName = fileNamesTmp.get(0).toString();
}
if (fileNamesTmp.size() > 1) {
long lastFileTime = 0;
for (int i = 0; i < fileNamesTmp.size(); i++) {
File lastTimeFile = new File((String) fileNamesTmp.get(i));
if (lastTimeFile.exists()) {
if (lastTimeFile.lastModified() > lastFileTime) {
fileName = (String) fileNamesTmp.get(i);
lastFileTime = lastTimeFile.lastModified();
System.out.println("fileNames=" + fileName);
}
}
}
}
logger.info("fileNames=" + fileName);
if (StringUtils.isNotEmpty(fileName)) {
File picFile = new File(fileName);
try {
if (picFile.exists()) {
byte[] buffer1 = getBytesFromFile(picFile);
if (picType == 5) {
return buffer1;
} else if (picType == 6) {
byte[] buffer2 = Base64.decode(buffer1, 0, buffer1.length);
for (int i = 0; i < buffer2.length; ++i) {
byte b = buffer2[i];
buffer2[i] = (byte) (((b & 0xf) << 4) | ((b & 0xf0) >> 4));
}
return buffer2;
}
} else {
return getDefalutPic();
}
} catch (Exception e) {
e.printStackTrace();
return getDefalutPic();
}
} else {
byte[] buffer2 = getConsolePic(date, serviceCode, certID, 6);
return buffer2;
}
return getDefalutPic();
}
/**
* 取得默认照片
*
* @return
*/
public static byte[] getDefalutPic() {
byte[] buffer = new byte[1024];
String path = PropertiesTools.getConfigProperties("picturePath") + "console.jpg";
File file = new File(path);
try {
if (file.exists()) {
logger.error("get certificate picture");
buffer = getBytesFromFile(file);
}
} catch (Exception e) {
e.printStackTrace();
return buffer;
}
return buffer;
}
/**
* 获取文件的字节
* @param file
* @return
* @throws IOException
*/
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
}