Java Code Examples for net.coobird.thumbnailator.Thumbnails#of()

The following examples show how to use net.coobird.thumbnailator.Thumbnails#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QRCodeUtils.java    From qrcodecore with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 生成带有logo的二维码
 * @param text 生成文本
 * @param logoInputStream logo文件输入流
 * @param qrCodeWidth 二维码宽度
 * @param qrCodeHeight 二维码高度
 * @param logoWidth logo宽度
 * @param logoHeight logo高度
 * @param ENCODE_HINTS 二维码配置
 * @return 输出流
 * @throws WriterException
 * @throws IOException
 */
private static Thumbnails.Builder<BufferedImage> createWidthLogo(String text, InputStream logoInputStream, int qrCodeWidth, int qrCodeHeight, int logoWidth, int logoHeight,
                                            String format, Map<EncodeHintType, Object> ENCODE_HINTS) throws WriterException, IOException {
    BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, ENCODE_HINTS);
    /* 生成二维码的bufferedImage */
    BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(bitMatrix);
    /* 创建图片构件对象 */
    Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB));
    BufferedImage logo = ImageIO.read(logoInputStream);
    BufferedImage logoImage = Thumbnails.of(logo).size(logoWidth, logoHeight).asBufferedImage();
    /* 设置logo水印位置居中,不透明  */
    builder.watermark(Positions.CENTER, qrcode, 1F)
            .watermark(Positions.CENTER, logoImage, 1F)
            .scale(1F)
            .outputFormat(format);
    return builder;
}
 
Example 2
Source File: ImageUtil.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public static OutputStream toOutputStream(InputStream input, OutputStream output, ImageParamDTO params)
    throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    if (null == builder) {
        return null;
    }

    try {
        fillBuilderWithParams(builder, params);
        builder.toOutputStream(output);
    } catch (IOException e) {
        logger.error("图片处理失败\n" + e.getMessage());
        throw e;
    }

    return output;
}
 
Example 3
Source File: AttachmentService.java    From wetech-cms with MIT License 5 votes vote down vote up
private void addFile(Attachment a,InputStream is) throws IOException {
		//进行文件的存储
		String realPath = SystemContext.getRealPath();
		String path = realPath+"/resources/upload/";
		String thumbPath = realPath+"/resources/upload/thumbnail/";
		File fp = new File(path);
		File tfp = new File(thumbPath);
//		System.out.println(fp.exists());
//		System.out.println(tfp.exists());
		if(!fp.exists()) fp.mkdirs();
		if(!tfp.exists()) tfp.mkdirs();
		path = path+a.getNewName();
		thumbPath = thumbPath+a.getNewName();
//		System.out.println(path+","+thumbPath);
		if(a.getIsImg()==1) {
			BufferedImage oldBi = ImageIO.read(is);
			int width = oldBi.getWidth();
			Builder<BufferedImage> bf = Thumbnails.of(oldBi);
			if(width>IMG_WIDTH) {
				bf.scale((double)IMG_WIDTH/(double)width);
			} else {
				bf.scale(1.0f);
			}
			bf.toFile(path);
			//缩略图的处理
			//1、将原图进行压缩
			BufferedImage tbi = Thumbnails.of(oldBi)
						.scale((THUMBNAIL_WIDTH*1.2)/width).asBufferedImage();
			//2、进行切割并且保持
			Thumbnails.of(tbi).scale(1.0f)
				.sourceRegion(Positions.CENTER, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT)
				.toFile(thumbPath);
		} else {
			FileUtils.copyInputStreamToFile(is, new File(path));
		}
	}
 
Example 4
Source File: Thumb.java    From HongsCORE with MIT License 5 votes vote down vote up
/**
 * 按尺寸缩放
 * 当 f 为 true 时强制拉伸到 w*h, 为 false 则等比缩放到 w*h
 * @param w
 * @param h
 * @param f
 * @return
 */
public Builder size(int w, int h, boolean f) {
    int sw = src.getWidth ();
    int sh = src.getHeight();

    BufferedImage img = draw(src, col, sw, sh);

    Builder bud = Thumbnails.of(img);
    if (f) {
        return  bud.forceSize  (w,h);
    } else {
        return  bud.size(w,h);
    }
}
 
Example 5
Source File: ImageUtil.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void toFile(String oldFile, String newFile, ImageParamDTO params) throws IOException {
    if (StringUtils.isBlank(oldFile) || StringUtils.isBlank(newFile)) {
        logger.error("原文件名或目标文件名为空");
        return;
    }
    Thumbnails.Builder builder = Thumbnails.of(oldFile);
    fillBuilderWithParams(builder, params);
    if (null == builder) {
        return;
    }
    builder.toFile(newFile);
}
 
Example 6
Source File: Thumb.java    From HongsCORE with MIT License 5 votes vote down vote up
/**
 * 转为构造器
 * 会创建新图, 会铺背景色
 * @return
 */
public Builder make() {
    int sw = src.getWidth ();
    int sh = src.getHeight();

    BufferedImage img = draw(src, col, sw, sh);

    return Thumbnails.of(img);
}
 
Example 7
Source File: ImageUtil.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static BufferedImage toBufferedImage(String oldFile, ImageParamDTO params) throws IOException {
    if (StringUtils.isBlank(oldFile)) {
        logger.error("原文件名或目标文件名为空");
        return null;
    }
    Thumbnails.Builder builder = Thumbnails.of(oldFile);
    fillBuilderWithParams(builder, params);
    if (null == builder) {
        return null;
    }
    return builder.asBufferedImage();
}
 
Example 8
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toFile(File input, File output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 9
Source File: FileDownloader.java    From rebuild with GNU General Public License v3.0 4 votes vote down vote up
@RequestMapping(value = "img/**", method = RequestMethod.GET)
public void viewImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
	String filePath = request.getRequestURI();
	filePath = filePath.split("/filex/img/")[1];
	
	ServletUtils.addCacheHead(response, IMG_CACHE_TIME);

	if (filePath.startsWith("http://") || filePath.startsWith("https://")) {
		response.sendRedirect(filePath);
		return;
	}

	final boolean temp = BooleanUtils.toBoolean(request.getParameter("temp"));
	String imageView2 = request.getQueryString();
	if (imageView2 != null && !imageView2.startsWith("imageView2")) {
		imageView2 = null;
	}

	// Local storage || temp
	if (!QiniuCloud.instance().available() || temp) {
		String fileName = QiniuCloud.parseFileName(filePath);
		String mimeType = request.getServletContext().getMimeType(fileName);
		if (mimeType != null) {
			response.setContentType(mimeType);
		}

		final int wh = imageView2 == null ? 0 : parseWidth(imageView2);

		// 原图
		if (wh <= 0 || wh >= 1000) {
			writeLocalFile(filePath, temp, response);
		}
		// 粗略图
		else {
			filePath = CodecUtils.urlDecode(filePath);
			File img = temp ? SysConfiguration.getFileOfTemp(filePath) : SysConfiguration.getFileOfData(filePath);

			BufferedImage bi = ImageIO.read(img);
			Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bi);

			if (bi.getWidth() > wh) {
				builder.size(wh, wh);
			} else {
				builder.scale(1.0);
			}

			builder
					.outputFormat(mimeType != null && mimeType.contains("png") ? "png" : "jpg")
					.toOutputStream(response.getOutputStream());
		}
	}
	else {
		if (imageView2 != null) {
			filePath += "?" + imageView2;
		}

		String privateUrl = QiniuCloud.instance().url(filePath, IMG_CACHE_TIME * 60);
		response.sendRedirect(privateUrl);
	}
}
 
Example 10
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toOutputStream(String input, OutputStream output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toOutputStream(output);
}
 
Example 11
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toFile(BufferedImage input, File output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 12
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toFile(String input, String output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 13
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toOutputStream(BufferedImage input, OutputStream output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toOutputStream(output);
}
 
Example 14
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toOutputStream(InputStream input, OutputStream output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toOutputStream(output);
}
 
Example 15
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toOutputStream(File input, OutputStream output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toOutputStream(output);
}
 
Example 16
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toOutputStream(String input, OutputStream output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toOutputStream(output);
}
 
Example 17
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toFile(BufferedImage input, File output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 18
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toFile(InputStream input, File output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 19
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toFile(File input, File output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 20
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void toFile(String input, String output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}