Java Code Examples for net.coobird.thumbnailator.Thumbnails#Builder

The following examples show how to use net.coobird.thumbnailator.Thumbnails#Builder . 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: UserAvatar.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 头像裁剪
 *
 * @param avatar
 * @param params x,y,width,height
 * @return
 * @throws IOException
 */
private String avatarCrop(File avatar, String params) throws IOException {
	String[] xywh = params.split(",");
	int x = Integer.parseInt(xywh[0]);
	int y = Integer.parseInt(xywh[1]);
	int width = Integer.parseInt(xywh[2]);
	int height = Integer.parseInt(xywh[3]);

	Thumbnails.Builder<File> builder = Thumbnails.of(avatar)
			.sourceRegion(x, y, width, height);

	String destName = System.currentTimeMillis() + avatar.getName();
	File dest;
	if (QiniuCloud.instance().available()) {
		dest = SysConfiguration.getFileOfTemp(destName);
	} else {
		dest = SysConfiguration.getFileOfData(destName);
	}
	builder.scale(1.0).toFile(dest);

	if (QiniuCloud.instance().available()) {
		destName = QiniuCloud.instance().upload(dest);
	}
	return destName;
}
 
Example 3
Source File: ImageUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
private static void fillBuilderWithParams(Thumbnails.Builder builder, ImageParam params) {
    builder.scale(1.0);

    if (params == null) {
        return;
    }
    // 按照一定规则改变原图尺寸
    if (null != params.getWidth() && null != params.getHeight()) {
        builder.size(params.getWidth(), params.getHeight());
    } else if (null != params.getXscale() && null != params.getYscale()) {
        builder.scale(params.getXscale(), params.getYscale());
    } else if (null != params.getScale()) {
        builder.scale(params.getScale(), params.getScale());
    }

    // 设置图片旋转角度
    if (null != params.getRotate()) {
        builder.rotate(params.getRotate());
    }

    // 设置图片压缩质量
    if (null != params.getQuality()) {
        builder.outputQuality(params.getQuality());
    }

    // 设置图片格式
    if (null != params.getFormat()) {
        builder.outputFormat(params.getFormat().name());
    }

    // 设置水印
    if (null != params.getWaterMark()) {
        Positions pos = getPostionsByCode(params.getWaterMark().getPosition());
        builder.watermark(pos, params.getWaterMark().getImage(), params.getWaterMark().getOpacity());
    }
}
 
Example 4
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 5
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 6
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 7
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 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 toFiles(List<File> input, List<File> output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.fromFiles(input);
    fillBuilderWithParams(builder, params);
    builder.toFiles(output);
}
 
Example 9
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 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 toFile(File input, File output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 11
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 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 toOutputStreams(List<InputStream> input, List<OutputStream> output, ImageParam params)
    throws IOException {
    Thumbnails.Builder builder = Thumbnails.fromInputStreams(input);
    fillBuilderWithParams(builder, params);
    builder.toOutputStreams(output);
}
 
Example 14
Source File: ThumbnailatorImageScaler.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
@Override
protected BufferedImage scaleImageCore(BufferedImage image, int targetWidth, int targetHeight,
        Map<String, Object> options) throws IOException {
    Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(image).size(targetWidth, targetHeight);
    ScalingMode filter = getFilter(options);
    if (filter != null) {
        builder = builder.scalingMode(getFilter(options));
    }
    Boolean dithering = getDithering(options);
    if (dithering != null) {
        builder = builder.dithering(dithering ? Dithering.ENABLE : Dithering.DISABLE);
    }
    Boolean antialiasing = getAntialiasing(options);
    if (antialiasing != null) {
        builder = builder.antialiasing(antialiasing ? Antialiasing.ON : Antialiasing.OFF);
    }

    // FIXME?: Thumbnailator doesn't appear to preserve the ColorModel of indexed images nor suppport setting it.
    // so for indexed we have no choice but to pass a hardcoded RGB type, which
    // will lose the original color model... oh well?

    // FIXME?: can do the TYPE_PRESERVING logic better here...

    ImageType targetType = getMergedTargetImageType(options, ImageType.EMPTY);
    ImageTypeInfo targetTypeInfo = targetType.getImageTypeInfoFor(image);

    BufferedImage resultImage;
    if (!ImagePixelType.isTypeNoPreserveOrNull(targetTypeInfo.getPixelType())) {
        ImageTypeInfo resolvedTargetTypeInfo = ImageType.resolveTargetType(targetTypeInfo, image);

        if (isNativeSupportedDestImageType(resolvedTargetTypeInfo)) {
            // here lib will _probably_ support the type we want...
            builder.imageType(resolvedTargetTypeInfo.getPixelType());
            resultImage = builder.asBufferedImage();
        } else {
            if (isPostConvertResultImage(image, options, targetTypeInfo)) {
                // for thumbnailator, we must always specify imageType because its default is to preserve and that doesn't work
                builder.imageType(ImageType.DEFAULT_DIRECT.getPixelTypeFor(image));
                resultImage = builder.asBufferedImage();
                resultImage = checkConvertResultImageType(image, resultImage, options, targetTypeInfo);
            } else {
                builder.imageType(getFirstSupportedDestPixelTypeFromAllDefaults(options, image));
                resultImage = builder.asBufferedImage();
            }
        }
    } else {
        builder.imageType(getFirstSupportedDestPixelTypeFromAllDefaults(options, image));
        resultImage = builder.asBufferedImage();
    }
    return resultImage;
}
 
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(InputStream 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(File 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 toOutputStreams(List<InputStream> input, List<OutputStream> output, ImageParam params)
    throws IOException {
    Thumbnails.Builder builder = Thumbnails.fromInputStreams(input);
    fillBuilderWithParams(builder, params);
    builder.toOutputStreams(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 toFiles(List<File> input, List<File> output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.fromFiles(input);
    fillBuilderWithParams(builder, params);
    builder.toFiles(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(InputStream input, File output, ImageParam params) throws IOException {
    Thumbnails.Builder builder = Thumbnails.of(input);
    fillBuilderWithParams(builder, params);
    builder.toFile(output);
}
 
Example 20
Source File: ImageUtils.java    From mblog with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 截图
 *
 * @param builder  Thumbnails.of
 * @param position the position
 * @param width    width
 * @param height   height
 * @param <T>      T
 * @throws IOException          IOException
 * @throws InterruptedException InterruptedException
 */
public static <T> byte[] screenshot(Thumbnails.Builder<T> builder, Position position, int width, int height) throws IOException, InterruptedException {
    BufferedImage image = builder.size(width, height).asBufferedImage();
    image = Thumbnails.of(image).size(width, height).crop(position).asBufferedImage();
    return toByte(image);
}