fix: 优化图片检查

master
wyb 5 months ago
parent 79ee0a2eb3
commit d7743ec1ef

@ -0,0 +1,157 @@
package com.docus.server.archive;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*
*
* @author WYBDEV
*/
public class SimpleImageInfo {
private int height;
private int width;
private String mimeType;
public SimpleImageInfo(File file) throws IOException {
InputStream is = new FileInputStream(file);
try {
processStream(is);
} finally {
is.close();
}
}
public SimpleImageInfo(InputStream is) throws IOException {
processStream(is);
}
public SimpleImageInfo(byte[] bytes) throws IOException {
InputStream is = new ByteArrayInputStream(bytes);
try {
processStream(is);
} finally {
is.close();
}
}
private void processStream(InputStream is) throws IOException {
int c1 = is.read();
int c2 = is.read();
int c3 = is.read();
mimeType = null;
width = height = -1;
if (c1 == 'G' && c2 == 'I' && c3 == 'F') { // GIF
is.skip(3);
width = readInt(is, 2, false);
height = readInt(is, 2, false);
mimeType = "image/gif";
} else if (c1 == 0xFF && c2 == 0xD8) { // JPG
while (c3 == 255) {
int marker = is.read();
int len = readInt(is, 2, true);
if (marker == 192 || marker == 193 || marker == 194) {
is.skip(1);
height = readInt(is, 2, true);
width = readInt(is, 2, true);
mimeType = "image/jpeg";
break;
}
is.skip(len - 2);
c3 = is.read();
}
} else if (c1 == 137 && c2 == 80 && c3 == 78) { // PNG
is.skip(15);
width = readInt(is, 2, true);
is.skip(2);
height = readInt(is, 2, true);
mimeType = "image/png";
} else if (c1 == 66 && c2 == 77) { // BMP
is.skip(15);
width = readInt(is, 2, false);
is.skip(2);
height = readInt(is, 2, false);
mimeType = "image/bmp";
} else {
int c4 = is.read();
if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
|| (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { //TIFF
boolean bigEndian = c1 == 'M';
int ifd = 0;
int entries;
ifd = readInt(is, 4, bigEndian);
is.skip(ifd - 8);
entries = readInt(is, 2, bigEndian);
for (int i = 1; i <= entries; i++) {
int tag = readInt(is, 2, bigEndian);
int fieldType = readInt(is, 2, bigEndian);
int valOffset;
if ((fieldType == 3 || fieldType == 8)) {
valOffset = readInt(is, 2, bigEndian);
is.skip(2);
} else {
valOffset = readInt(is, 4, bigEndian);
}
if (tag == 256) {
width = valOffset;
} else if (tag == 257) {
height = valOffset;
}
if (width != -1 && height != -1) {
mimeType = "image/tiff";
break;
}
}
}
}
if (mimeType == null) {
throw new IOException("Unsupported image type");
}
}
private int readInt(InputStream is, int noOfBytes, boolean bigEndian) throws IOException {
int ret = 0;
int sv = bigEndian ? ((noOfBytes - 1) * 8) : 0;
int cnt = bigEndian ? -8 : 8;
for (int i = 0; i < noOfBytes; i++) {
ret |= is.read() << sv;
sv += cnt;
}
return ret;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
@Override
public String toString() {
return "MIME Type : " + mimeType + "\t Width : " + width
+ "\t Height : " + height;
}
}

@ -1,6 +1,7 @@
package com.docus.server.archive.service.impl;
import com.docus.core.util.Func;
import com.docus.server.archive.SimpleImageInfo;
import com.docus.server.archive.entity.ScanAssort;
import com.docus.server.archive.entity.TBasic;
import com.docus.server.archive.mapper.BasicMapper;
@ -16,6 +17,7 @@ import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
@ -76,29 +78,52 @@ public class LianzhongCollectCheckServiceImpl implements LianzhongCollectCheckSe
}
}
/**
* true
*/
private boolean hasDamage(List<ScanAssort> scanAssortList) {
try {
for (ScanAssort assort : scanAssortList) {
String filePath = assort.getImagePath() + File.separator + assort.getScanPage();
BufferedImage image = ImageIO.read(new File(filePath));
if (image.getHeight() <= 0 || image.getWidth() <= 0) {
return true;
}
for (ScanAssort assort : scanAssortList) {
String filePath = assort.getImagePath() + File.separator + assort.getScanPage();
File file = new File(filePath);
if (!isOpenImage(file)) {
return true;
}
return false;
}
return false;
}
private boolean isOpenImage(File file) {
try {
SimpleImageInfo imageInfo = new SimpleImageInfo(file);
return imageInfo.getHeight() > 0 && imageInfo.getWidth() > 0;
} catch (Exception ex) {
return true;
try {
BufferedImage image = ImageIO.read(file);
return image.getHeight() > 0 && image.getHeight() > 0;
} catch (IOException e) {
return false;
}
}
}
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
System.out.println(sdf.format(new Date()));
ScanAssort scanAssort = new ScanAssort();
scanAssort.setImagePath("C:\\Users\\wyb\\Pictures\\联想截图");
scanAssort.setScanPage("yuai2.png");
scanAssort.setScanPage("yuai.png");
List<ScanAssort> scanAssortList = Arrays.asList(scanAssort);
System.out.println(new LianzhongCollectCheckServiceImpl().hasDamage(scanAssortList));
long start = System.currentTimeMillis();
LianzhongCollectCheckServiceImpl checkService = new LianzhongCollectCheckServiceImpl();
for (int i = 0; i < 1000; i++) {
boolean b = checkService.hasDamage(scanAssortList);
if (i == 99) {
System.out.println(checkService.hasDamage(scanAssortList));
}
}
long total = System.currentTimeMillis() - start;
System.out.println("总共耗时 " + total + "ms");
}
}

Loading…
Cancel
Save