1. MultipartFile转换BufferedImage
public static void MultipartFileHeight(MultipartFile file) throws IOException {
BufferedImage image = ImageIO.read(file.getInputStream());
}
2,InputStream转换byte[]
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
3,网络图片路径http变成流
public static InputStream getInputStreamByGet(String url) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(50000);
conn.setConnectTimeout(50000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
return inputStream;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
4,网络地址https图片
/**
* 网络地址https图片
* @param url
* @return
* @throws Exception
*/
public static BufferedImage HttpsUtils(String url) throws Exception {
// String url = "https://visualhunt.com/photos/1/aerial-view-of-laptop-notebook-mobile-phone-and-coffee-cup-on-wooden-table-1.jpg?s=xl2";
URI uri = URI.create(url);
HttpGet httpGet = new HttpGet(uri);
HelloWord.SSLClient httpclient = new HelloWord.SSLClient();
httpclient.execute(httpGet, response -> {
HttpEntity entity = response.getEntity();
BufferedImage bufferedImage = ImageIO.read(entity.getContent());
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
System.out.println("width = " + width);
System.out.println("height = " + height);
// if (entity != null) {//存本地
// System.out.println("entity = " + entity);
// FileCopyUtils.copy(entity.getContent(),new FileOutputStream("D:\\imgags\\test.jpg"));
// }
BufferedImage image= ImageIO.read(entity.getContent());
return bufferedImage;
});
return null;
}
public static class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception {
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
5,获取指定视频的帧并保存为图片至指定目录
public static void fetchFrame(String videofile, String framefile)
throws Exception {
long start = System.currentTimeMillis();
File targetFile = new File(framefile);
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
ff.start();
int lenght = ff.getLengthInFrames();
int i = 0;
Frame f = null;
while (i < lenght) {
// 过滤前5帧,避免出现全黑的图片,依自己情况而定
f = ff.grabFrame();
if ((i > 48) && (f.image != null)) {
break;
}
i++;
}
// IplImage img = f.image;
int owidth = f.imageWidth;
int oheight = f.imageHeight;
// 对截取的帧进行等比例缩放
int width = 300;
int height = (int) (((double) width / owidth) * oheight);
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage fecthedImage = converter.getBufferedImage(f);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(fecthedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ImageIO.write(bi, "jpg", targetFile);
ff.stop();
System.out.println(System.currentTimeMillis() - start);
}
6,BufferedImage转换InputStream
BufferedImage bi=new BufferedImage();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", os);
InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
7,MultipartFile转换file
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
继续更新中