Guwan-smartSchool/shapelight-admin/src/test/java/gb/CarImageTest.java

104 lines
3.8 KiB
Java
Raw Normal View History

package gb;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import net.coobird.thumbnailator.Thumbnails;
import net.shapelight.AdminApplication;
import net.shapelight.common.config.MinioConfig;
import net.shapelight.common.utils.ImageUtils;
import net.shapelight.common.utils.StringUtils;
import net.shapelight.common.utils.ThumbnailsUtils;
import net.shapelight.common.utils.UUIDUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class)
public class CarImageTest {
@Autowired
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
@Test
public void ossTest() {
String imgUrl = "http://icecloud-prod.oss-cn-shenzhen.aliyuncs.com/P1610092525/image/202104/25/%E9%99%95A207AY_out_2_5802.jpg?Expires=1619330640&OSSAccessKeyId=LTAI4Furp7fmTGDMRhJ95jaX&Signature=2xS3T%2FlK9LMe%2FjAOotCEvIlCOmU%3D";
imgUrl = imgUrl.replace("amp;", "");
String fileName = "car/" + 222 + "/" + UUIDUtil.uuid() + ".jpg";
if (!StringUtils.isEmpty(imgUrl)) {
URL url = null;
InputStream is = null;
HttpURLConnection httpUrl = null;
try {
url = new URL(imgUrl);
BufferedImage image = ImageIO.read(url);
//获取图片的宽、高
System.out.println("Width: " + image.getWidth());
System.out.println("Height: " + image.getHeight());
//调整图片大小为 400X400尺寸
// BufferedImage newImage = ImageUtils.resizeImage(image,400,400);
int w = image.getWidth();
int h = image.getHeight();
if(w>1024 || h>1024){
image = ThumbnailsUtils.resizeImageOne(image,1024,1024);
}
// BufferedImage resizeBuff = ThumbnailsUtils.resizeImageOne(image,1024,1024);
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// if (w > 1024 || h > 1024) {
// Thumbnails.of(image)
// .size(1024, 1024)
//// .outputFormat("JPEG")
//// .outputQuality(1)
// .toOutputStream(outputStream);
// }
byte[] data = ImageUtils.imageToBytes(image);
// ByteArrayInputStream
is = new ByteArrayInputStream(data);
int rl = is.available();
// httpUrl = (HttpURLConnection) url.openConnection();
// httpUrl.connect();
//// httpUrl.getInputStream();
// is = httpUrl.getInputStream();
// int rl = httpUrl.getContentLength();
// int l = is.available();
PutObjectOptions putObjectOptions = new PutObjectOptions(rl, -1L);
putObjectOptions.setContentType("image/jpeg");
this.minioClient.putObject(this.minioConfig
.getBucketName(), fileName, is, putObjectOptions);
} catch (Exception e) {
e.printStackTrace();
fileName = "";
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if (httpUrl != null)
httpUrl.disconnect();
}
}
}
}