提交 37334b6f 编写于 作者: C cjm_01

Fri Aug 4 11:23:00 CST 2023 inscode

上级 a5df1b10
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import nc.bs.logging.Logger;
import nc.vo.pub.BusinessException;
import org.apache.commons.net.util.Base64;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.xmlbeans.impl.store.Path;
import sun.misc.BASE64Encoder;
import com.informix.util.dateUtil;
import com.lowagie.text.pdf.codec.Base64.OutputStream;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;
@SuppressWarnings("all")
public class NyImageUtils {
public static List<String> getImageToString(JComponent jComponent) throws IOException, BusinessException {
List<String> list = new ArrayList<>();
File file = getFile(jComponent);
if (file == null) {
return null;
}
Logger.error("开始读取文件:" + file.getName());
if (file.getName().endsWith(".pdf")) {
list = pdfToImage(file);
} else if (file.getName().endsWith(".bmp")) {
list.add(Base64.encodeBase64String(bmpToImage(file)));
} else {
byte[] bytes = imageToBytes(file);
list.add(Base64.encodeBase64String(bytes));
}
return list;
}
private static byte[] bmpToImage(File file) {
byte[] data = null;
File newfile = null;
String result = null;
try {
String path = file.getPath();
result = path.replace("bmp", "jpg");
FileOutputStream out = new FileOutputStream(result);
newfile = new File(result);
ImageIO.write(ImageIO.read(file), "jpg", newfile);
data = imageToBytes(newfile);
// Files.deleteIfExists(Paths.get(result));
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
public static List<String> sprPdfToImage_stream(byte[] returnBytes) throws BusinessException {
List<String> list = new ArrayList<String>();
PdfDocument pdf = new PdfDocument();
pdf.loadFromBytes(returnBytes);
for (int i = 0; i < pdf.getPages().getCount(); i++) {
// 将页面保存为图片,并设置DPI分辨率
BufferedImage image = pdf.saveAsImage(i, PdfImageType.Bitmap, 500, 500);
// 将图片保存为png格式
ByteArrayOutputStream fileByte = new ByteArrayOutputStream();
try {
ImageIO.write(image, "PNG", fileByte);
byte[] bytes = fileByte.toByteArray();
list.add(Base64.encodeBase64String(bytes));
} catch (IOException e) {
String msg = "解析pdf文件出错," + e.getMessage();
Logger.error(msg);
throw new BusinessException(msg);
} finally {
pdf.close();
}
}
return list;
}
public static List<String> pdfToImage_Sprpdf(File file) throws BusinessException {
List<String> list = new ArrayList<String>();
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(file.toString());
for (int i = 0; i < pdf.getPages().getCount(); i++) {
// 将页面保存为图片,并设置DPI分辨率
BufferedImage image = pdf.saveAsImage(i, PdfImageType.Bitmap, 500, 500);
// 将图片保存为png格式
ByteArrayOutputStream fileByte = new ByteArrayOutputStream();
try {
ImageIO.write(image, "PNG", fileByte);
byte[] bytes = fileByte.toByteArray();
list.add(Base64.encodeBase64String(bytes));
} catch (IOException e) {
String msg = "解析pdf文件出错," + e.getMessage();
Logger.error(msg);
throw new BusinessException(msg);
} finally {
pdf.close();
}
}
return list;
}
public static List<String> pdfToImage(File file) throws BusinessException {
List<String> list = new ArrayList<String>();
PDDocument doc = null;
try {
doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImage(i, 2.0f);
ByteArrayOutputStream fileByte = new ByteArrayOutputStream();
ImageIO.write(image, "png", fileByte);
byte[] bytes = fileByte.toByteArray();
list.add(Base64.encodeBase64String(bytes));
}
return list;
} catch (IOException e) {
String msg = "解析pdf文件出错," + e.getMessage();
Logger.error(msg);
throw new BusinessException(msg);
} finally {
if (null != doc) {
try {
doc.close();
} catch (IOException e) {
}
}
}
}
private static byte[] imageToBytes(File file) throws IOException {
if (file == null || !file.exists() || !file.isFile()) {
return new byte[0];
}
byte[] bytes = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
bytes = new byte[(int) file.length()];
fis.read(bytes);
return bytes;
} finally {
if (null != fis) {
fis.close();
}
}
}
private static File getFile(JComponent jComponent) {
File file = null;
JFileChooser choose = new JFileChooser();
choose.setAcceptAllFileFilterUsed(false);
choose.addChoosableFileFilter(new FileFilter() {
public boolean accept(File f) {
String fileName = f.getName();
return f.isDirectory() || fileName.endsWith(".pdf") || fileName.endsWith(".PDF") || fileName.endsWith(".gif") || fileName.endsWith(".GIF")
|| fileName.endsWith(".jpg") || fileName.endsWith(".JPG") || fileName.endsWith(".bmp") || fileName.endsWith(".BMP")
|| fileName.endsWith(".jpeg") || fileName.endsWith(".JPEG") || fileName.endsWith(".png") || fileName.endsWith(".PNG");
}
public String getDescription() {
return "支持的文件格式:.pdf .gif .jpg .bmp .png .jpeg ";
}
});
int retValue = choose.showOpenDialog(jComponent);
if (retValue == JFileChooser.APPROVE_OPTION) {
String filePath = ((File) choose.getSelectedFile()).getAbsolutePath();
/*
* if (filePath == null || "".equals(filePath) ||
* (filePath.indexOf(".xls") < 0 && filePath.indexOf(".xlsx") < 0))
* { MessageDialog.showErrorDlg(jComponent, "错误", "请选择EXCEL文件!");
* return null; }
*/
file = new File(filePath);
}
return file;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册