
@RestController
@RequestMapping("file")
public class ImageController extends baseController {
private static final long serialVersionUID = 1L;
private static BufferedImage intputimage;
private static BufferedImage outimage;
private static String filename;
private static Integer maxrgb;
@Autowired
Environment environment;
@Autowired
SessionStrategy sessionStrategy;
@Autowired
ImageUtils imageutils;
@RequestMapping(value = "/getimagecode", method = RequestMethod.GET)
public void getImageCode(HttpServletRequest request,HttpServletResponse response){
try {
response.setContentType("image/jpeg");
ImageCode imagecode = imageutils.getImageCode();
sessionStrategy.setAttribute(new ServletWebRequest(request),Constants.session_key,imagecode);
ImageIO.write(imagecode.getImage(),"JPEG",response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/pasebackground/{keys:\d+}/{types:\d?}/{rgbs}/{checked}", method = RequestMethod.GET)
public void pasebackground(
@PathVariable("keys") Integer keys,
@PathVariable("types") Integer types,
@PathVariable("rgbs") String rgbs,
@PathVariable("checked") Boolean checked,
HttpServletResponse response
) {
if (intputimage == null)throw new RuntimeException("请上传图片文件");
try {
response.setContentType("image/png");
ImageIO.write((outimage = getImage(null,(outimage!=null && checked?outimage:intputimage),keys,types,rgbs)),"PNG",response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/paseliangdu/{keys:\d+}/{checked}", method = RequestMethod.GET)
public void paseliangdu(
@PathVariable("keys") Integer keys,
@PathVariable("checked") Boolean checked,
HttpServletResponse response
) {
if (intputimage == null)throw new RuntimeException("请上传图片文件");
try {
response.setContentType("image/png");
ImageIO.write((outimage = liangdu(null,(outimage!=null && checked?outimage:intputimage),keys)),"PNG",response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/download/{filename}", method = RequestMethod.GET)
public void testDownload(HttpServletResponse resp, @PathVariable("filename") String name) throws IOException {
Boolean isimage = Objects.equals("this",name);
String file_name = LocalTime.now().getSecond()+".PNG";
if(filename!=null && filename.length()>0)
file_name = URLEncoder.encode(isimage?filename:name,"UTF-8");
if(isimage){
if(outimage!=null && filename!=null){
resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
resp.setHeader("Content-Disposition","attachment;filename=" + file_name);
ImageIO.write(outimage,"PNG",resp.getOutputStream());
}else{
resp.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
resp.getWriter().println(JSONObject.toJSonString(ResultUtil.resultErr("没有上传图片")));
}
return;
}
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition","attachment;filename=" + file_name);
File file = new File(environment.getProperty("file.upload.path"),name);
if(!file.exists()){resp.setStatus(404);return;}
Files.copy(file.toPath(),resp.getOutputStream());
}
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResultUtil> upload(HttpServletRequest request, HttpServletResponse response) {
Map multipartfiles = ((MultipartHttpServletRequest) request).getFileMap();
if (multipartfiles.isEmpty())throw new RuntimeException("文件不能为空");
for (Map.Entry file : multipartfiles.entrySet()) {
MultipartFile multipartFile = file.getValue();
if (multipartFile.isEmpty())throw new RuntimeException("文件不能为空");
filename = multipartFile.getOriginalFilename();
try {
if (multipartfiles.containsKey("header") && multipartFile.getContentType().matches("image/(jpeg|jpg|png|gif)")) {
if (multipartFile.getSize() > 1024 * 1024 * 1)throw new RuntimeException("图片大小不能超过1MB");
intputimage = ImageIO.read(multipartFile.getInputStream());
return ResultUtil.resultOK("上传成功");
}
String filepath = environment.getProperty(multipartfiles.containsKey("header")?"upload.header.path":"file.upload.path");
Path path = Paths.get(filepath, multipartFile.getOriginalFilename());
if (!Files.exists(path.getParent()))Files.createDirectories(path.getParent());
Files.write(path,multipartFile.getBytes(),StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
return ResultUtil.resultOK();
}
public static BufferedImage liangdu(String path, BufferedImage image,int random,int... maxrgb) {
BufferedImage images = null;
try {
if (path == null && image == null)throw new RuntimeException("path or image not null");
BufferedImage imagess = (image == null ? ImageIO.read(new File(path)) : image);
images = imagess
.createGraphics()
.getDeviceConfiguration()
.createCompatibleImage(
imagess.getWidth()
,imagess.getHeight(),
Transparency.TRANSLUCENT);
//int R = ((maxrgb[0] >> 16) & 0xff), G = ((maxrgb[0] >> 8) & 0xff), B = (maxrgb[0] & 0xff);
int[] rgbs = new int[imagess.getWidth() * imagess.getHeight()];
imagess.getRGB(0, 0, imagess.getWidth(), imagess.getHeight(), rgbs, 0, imagess.getWidth());
for (int i = 0; i < rgbs.length; i++) {
int r = ((rgbs[i] >> 16) & 0xff), g = ((rgbs[i] >> 8) & 0xff), b = (rgbs[i] & 0xff);
//if(maxrgb.length==2)
//if (Math.abs(R - r) < maxrgb[1] && Math.abs(G - g) < maxrgb[1] && Math.abs(B - b) < maxrgb[1])continue;
int R2 = r+random,G2 = g+random,B2 = b+random;
if(R2 > 230 && G2 > 230 && B2 > 230)continue;
rgbs[i] = ((255 & 0xff) << 24) | ((clamp(R2) & 0xff) << 16) | ((clamp(G2) & 0xff) << 8) | (clamp(B2) & 0xff);
}
images.setRGB(0,0,images.getWidth(),images.getHeight(),rgbs,0,images.getWidth());
} catch (Exception e) {
e.printStackTrace();
}
return images;
}
private static int clamp(int r) {
return r>255?255:r<0?0:r;
}
public static BufferedImage getImage(String path, BufferedImage image,int keys, int type,String rgbvalue) {
BufferedImage images2 = null;
try {
Map countmaps = new HashMap<>();
if (path == null && image == null)
throw new RuntimeException("path or image not null");
BufferedImage images = image == null ? ImageIO.read(new File(path)) : image;
int[] rgbs = new int[images.getWidth() * images.getHeight()];
images.getRGB(0, 0, images.getWidth(), images.getHeight(), rgbs, 0, images.getWidth());
for (int rgb : rgbs) {
if (!countmaps.containsKey(rgb)) {
countmaps.put(rgb, 0);
continue;
}
countmaps.put(rgb, countmaps.get(rgb) + 1);
}
Integer maxvalue = countmaps.values().stream().reduce(0, (x, y) -> x > y ? x : y);
Integer maxrgb = countmaps.keySet().stream().filter(x -> Objects.equals(countmaps.get(x), maxvalue))
.findFirst().orElse(null);
//System.err.println(maxrgb + " : " + maxvalue);
Graphics2D graphics = images.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
images2 = graphics.getDeviceConfiguration().createCompatibleImage(images.getWidth(), images.getHeight(),
Transparency.TRANSLUCENT);
int R = ((maxrgb >> 16) & 0xff), G = ((maxrgb >> 8) & 0xff), B = (maxrgb & 0xff);
for (int i = 0; i < images.getWidth(); i++) {
for (int j = 0; j < images.getHeight(); j++) {
int rgb = images.getRGB(i, j);
int r = ((rgb >> 16) & 0xff), g = ((rgb >> 8) & 0xff), b = (rgb & 0xff);
if(type==0){
if (!(Math.abs(R - r) < keys && Math.abs(G - g) < keys && Math.abs(B - b) < keys))
images2.setRGB(i, j, rgb);
}else if(type==2){
if(rgbvalue==null || rgbvalue.length()<1 || !rgbvalue.matches("(\d+,\d+,\d+)"))
throw new RuntimeException("rgbvalue有误");
String[] rgb_value = rgbvalue.split(",",3);
int R2 = Integer.parseInt(rgb_value[0]),G2 = Integer.parseInt(rgb_value[1]),B2 = Integer.parseInt(rgb_value[2]);
if (Math.abs(R - r) < keys && Math.abs(G - g) < keys && Math.abs(B - b) < keys){
images2.setRGB(i,j,new Color(R2,G2,B2).getRGB());
}else images2.setRGB(i,j,rgb);
}
// images.setRGB(i,j,((toumin*255/10)<<24) | (rgb &
// 0x00ffffff));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return images2;
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)