|
@@ -5,8 +5,8 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.aliyun.oss.ServiceException;
|
|
|
import com.yh.saas.plugin.yiliangyiyun.entity.*;
|
|
|
import com.yh.saas.plugin.yiliangyiyun.entity.view.DistinguishView;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
-import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.NameValuePair;
|
|
@@ -22,8 +22,13 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
+import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
+import sun.awt.image.BufferedImageGraphicsConfig;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
@@ -33,6 +38,7 @@ import java.text.DecimalFormat;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class EntityAnalyse {
|
|
|
|
|
@@ -505,9 +511,9 @@ public class EntityAnalyse {
|
|
|
params.put("insuranceInformation", insuranceInformation);
|
|
|
|
|
|
params.put("remark", orderInfo.getRemark() != null ? orderInfo.getRemark() : "");
|
|
|
- String[] file1 = {EntityAnalyse.uploadImage(EntityAnalyse.urlToFile(new URL(orderInfo.getDriverContractUrl())), orderInfo.getToken(), ENV)};
|
|
|
- params.put("file", file1);
|
|
|
- String[] ownersContractFile = {EntityAnalyse.uploadImage(EntityAnalyse.urlToFile(new URL(orderInfo.getContractUrl())), orderInfo.getToken(), ENV)};
|
|
|
+// String[] file1 = {EntityAnalyse.uploadImage(EntityAnalyse.urlToPdfFile(new URL(orderInfo.getDriverContractUrl())), orderInfo.getToken(), ENV)};
|
|
|
+// params.put("file", file1);
|
|
|
+ String[] ownersContractFile = {EntityAnalyse.uploadImage(EntityAnalyse.urlToPdfFile(new URL(orderInfo.getContractUrl())), orderInfo.getToken(), ENV)};
|
|
|
params.put("ownersContractFile", ownersContractFile);
|
|
|
String[] backDocumentImages = {EntityAnalyse.uploadImage(EntityAnalyse.urlToFile(new URL(orderInfo.getReceiptImg())), orderInfo.getToken(), ENV)};
|
|
|
params.put("backDocumentImages", backDocumentImages);
|
|
@@ -1332,42 +1338,173 @@ public class EntityAnalyse {
|
|
|
* @return
|
|
|
*/
|
|
|
public static File urlToFile(URL url) {
|
|
|
- return writeImageToDisk(getImageFromNetByUrl(url));
|
|
|
-// InputStream is = null;
|
|
|
-// File file = null;
|
|
|
-// FileOutputStream fos = null;
|
|
|
-// try {
|
|
|
-// String tmp = "templates" + File.separator + "tmp.jpg";
|
|
|
-// file = new File( new String(tmp.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
|
|
|
-//// file = File.createTempFile("tmp", null);
|
|
|
-// URLConnection urlConn = null;
|
|
|
-// urlConn = url.openConnection();
|
|
|
-// is = urlConn.getInputStream();
|
|
|
-// fos = new FileOutputStream(file);
|
|
|
-// byte[] buffer = new byte[4096];
|
|
|
-// int length;
|
|
|
-// while ((length = is.read(buffer)) > 0) {
|
|
|
-// fos.write(buffer, 0, length);
|
|
|
-// }
|
|
|
-// return file;
|
|
|
-// } catch (IOException e) {
|
|
|
-// return null;
|
|
|
-// } finally {
|
|
|
-// if (is != null) {
|
|
|
-// try {
|
|
|
-// is.close();
|
|
|
-// } catch (IOException e) {
|
|
|
-// }
|
|
|
-// }
|
|
|
-// if (fos != null) {
|
|
|
-// try {
|
|
|
-// fos.close();
|
|
|
-// } catch (IOException e) {
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
+ File file = writeImageToDisk(getImageFromNetByUrl(url));
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * url资源转化为file流
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static File urlToPdfFile(URL url) {
|
|
|
+ byte[] bytes = getImageFromNetByUrl(url);
|
|
|
+ File file = writePDFToDisk(bytes);
|
|
|
+ try {
|
|
|
+ PDDocument doc = PDDocument.load(file);
|
|
|
+ PDFRenderer renderer = new PDFRenderer(doc);
|
|
|
+ int pageCount = doc.getNumberOfPages();
|
|
|
+ BufferedImage image = null;
|
|
|
+ for (int i = 0; i < pageCount; i++) {
|
|
|
+ if(i == 0){
|
|
|
+ image = renderer.renderImageWithDPI(i, 72); // Windows native DPI
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ BufferedImage tmpImage = renderer.renderImageWithDPI(i, 72);
|
|
|
+ image = mergeImage(image,tmpImage,false,true,true,0,null);
|
|
|
+ }
|
|
|
+ // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
|
|
|
+ }
|
|
|
+ file = new File("templates" + File.separator + "tmpPdf.png");
|
|
|
+ ImageIO.write(image, "png", file);
|
|
|
+// Thumbnails.of(file) //原图片
|
|
|
+// .scale(0.4) //分辨率比例
|
|
|
+// .outputQuality(1) //图片质量
|
|
|
+// .outputFormat("JPEG") //目标文件格式
|
|
|
+// .toFile(file); //目标图片
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+// bytes = ImgCompressUtil.compressPicForScale(getImageStream(file),2000);
|
|
|
+// file = readBin2Image(bytes,file);
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+ public static byte[] getImageStream(File file) {
|
|
|
+ byte[] buffer = null;
|
|
|
+ FileInputStream fis;
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ fis = new FileInputStream(file);
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int n;
|
|
|
+ while ((n = fis.read(b)) != -1) {
|
|
|
+ bos.write(b, 0, n);
|
|
|
+ }
|
|
|
+ fis.close();
|
|
|
+ bos.close();
|
|
|
+ buffer = bos.toByteArray();
|
|
|
+ if(file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return buffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static File readBin2Image(byte[] byteArray, File file) {
|
|
|
+ InputStream in = new ByteArrayInputStream(byteArray);
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ try {
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
+ int len = 0;
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ while ((len = in.read(buf)) != -1) {
|
|
|
+ fos.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ fos.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (null != fos) {
|
|
|
+ try {
|
|
|
+ fos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return file;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 合并两个图片
|
|
|
+ * @param first 图片1
|
|
|
+ * @param second 图片2
|
|
|
+ * @param horizontal 等于true,则两个图片水平合并显示, 否则两个图片垂直合并显示
|
|
|
+ * @param center 图片是否水平居中, horizontal 等于 false 垂直合并才有效
|
|
|
+ * @param transparent 合并后的图片背景是否透明色
|
|
|
+ * @param gap 图片之间的间距
|
|
|
+ * @param color 图片的背景颜色
|
|
|
+ * @return 返回合并之后的图片
|
|
|
+ */
|
|
|
+ public static BufferedImage mergeImage(BufferedImage first, BufferedImage second, boolean horizontal, boolean center, boolean transparent, int gap, Color color) throws IOException {
|
|
|
+ if (first == null) {
|
|
|
+ return second;
|
|
|
+ } else if (second == null) {
|
|
|
+ return first;
|
|
|
+ }
|
|
|
+ // 获取原始图片宽度
|
|
|
+ int firstWidth = first.getWidth();
|
|
|
+ int firstHeight = first.getHeight();
|
|
|
+ int secondWidth = second.getWidth();
|
|
|
+ int secondHeight = second.getHeight();
|
|
|
+ int picGap = gap * 2;
|
|
|
+ // 合并后的图片宽高
|
|
|
+ int mergeWidth = Math.max(firstWidth, secondWidth) + picGap;
|
|
|
+ int mergeHeight = firstHeight + secondHeight + picGap;
|
|
|
+ if (horizontal) {
|
|
|
+ mergeWidth = firstWidth + secondWidth + picGap;
|
|
|
+ mergeHeight = Math.max(firstHeight, secondHeight) + picGap;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建目标图片对象
|
|
|
+ BufferedImage target = new BufferedImage(mergeWidth, mergeHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
+ if (transparent) {
|
|
|
+ // 设置图片背景为透明的
|
|
|
+ BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(target);
|
|
|
+ target = config.createCompatibleImage(mergeWidth, mergeHeight, Transparency.TRANSLUCENT);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建绘制目标图片对象
|
|
|
+ Graphics2D graphics = target.createGraphics();
|
|
|
+ int x1, y1;
|
|
|
+ int x2, y2;
|
|
|
+ if (horizontal) {
|
|
|
+ // 水平合并
|
|
|
+ x1 = gap;
|
|
|
+ y1 = gap;
|
|
|
+ x2 = firstWidth + gap;
|
|
|
+ y2 = gap;
|
|
|
+ } else {
|
|
|
+ // 垂直合并
|
|
|
+ if (center) {
|
|
|
+ // 计算居中位置
|
|
|
+ x1 = (mergeWidth - firstWidth) / 2;
|
|
|
+ x2 = (mergeWidth - secondWidth) / 2;
|
|
|
+ } else {
|
|
|
+ x1 = gap;
|
|
|
+ x2 = gap;
|
|
|
+ }
|
|
|
+ y1 = gap;
|
|
|
+ y2 = firstHeight + gap;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 图片的背景颜色
|
|
|
+ if (color != null) {
|
|
|
+ graphics.setColor(color);
|
|
|
+ graphics.fillRect(0, 0, mergeWidth, mergeHeight);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按照顺序绘制图片
|
|
|
+ graphics.drawImage(first, x1, y1, firstWidth, firstHeight, null);
|
|
|
+ graphics.drawImage(second, x2, y2, secondWidth, secondHeight, null);
|
|
|
+ graphics.dispose();
|
|
|
+ // 返回合并后的图片对象
|
|
|
+ return target;
|
|
|
+ }
|
|
|
/**
|
|
|
* 将获取的字节数组保存为文件写入硬盘
|
|
|
*
|
|
@@ -1391,7 +1528,29 @@ public class EntityAnalyse {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 将获取的字节数组保存为文件写入硬盘
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ public static File writePDFToDisk(byte[] data) {
|
|
|
+ try {
|
|
|
+ File file = new File("templates" + File.separator + "tmp.pdf"); // 本地目录
|
|
|
+ File fileParent = file.getParentFile();
|
|
|
+ if (!fileParent.exists()) {
|
|
|
+ fileParent.mkdirs();
|
|
|
+ file.createNewFile();
|
|
|
+ }
|
|
|
+ FileOutputStream fops = new FileOutputStream(file);
|
|
|
+ fops.write(data);
|
|
|
+ fops.flush();
|
|
|
+ fops.close();
|
|
|
+ return file;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 获取远程http地址视图片
|