How to use compressToThumbnail method of com.tngtech.jgiven.report.html5.Html5AttachmentGenerator class

Best JGiven code snippet using com.tngtech.jgiven.report.html5.Html5AttachmentGenerator.compressToThumbnail

Source:Html5AttachmentGenerator.java Github

copy

Full Screen

...115 try {116 if (attachment.isBinary()) {117 if (mediaType.is(MediaType.ANY_IMAGE_TYPE)) {118 File thumbFile = getThumbnailFileFor(targetFile);119 byte[] thumbnail = compressToThumbnail(attachment.getValue(), extension);120 Files.write(thumbnail, thumbFile);121 }122 Files.write(BaseEncoding.base64().decode(attachment.getValue()), targetFile);123 } else {124 Files.write(attachment.getValue().getBytes(Charsets.UTF_8), targetFile);125 if (com.tngtech.jgiven.attachment.MediaType.SVG_UTF_8.toString().equals(attachment.getMediaType())) {126 File thumbFile = getThumbnailFileFor(targetFile);127 writeThumbnailForSVG(targetFile, thumbFile);128 }129 }130 } catch (IOException e) {131 log.error("Error while trying to write attachment to file " + targetFile, e);132 }133 return targetFile;134 }135 private byte[] compressToThumbnail(String base64content, String extension) {136 byte[] imageBytes = BaseEncoding.base64().decode(base64content);137 byte[] base64thumb = {};138 try {139 BufferedImage before = ImageIO.read(new ByteArrayInputStream(imageBytes));140 BufferedImage after = scaleDown(before);141 base64thumb = bufferedImageToBase64(after, extension);142 } catch (IOException e) {143 log.error("Error while decoding the attachment to BufferedImage ", e);144 }145 return base64thumb;146 }147 static BufferedImage scaleDown(BufferedImage before) {148 double xFactor = Math.min(1.0, MINIMAL_THUMBNAIL_SIZE / (double) before.getWidth());149 double yFactor = Math.min(1.0, MINIMAL_THUMBNAIL_SIZE / (double) before.getHeight());150 double factor = Math.max(xFactor, yFactor);151 int width = (int) Math.round(before.getWidth() * factor);152 int height = (int) Math.round(before.getHeight() * factor);153 BufferedImage after = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);154 AffineTransform at = new AffineTransform();155 at.scale(factor, factor);156 AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);157 return scaleOp.filter(before, after);158 }159 private byte[] bufferedImageToBase64(BufferedImage bi, String extension) {160 ByteArrayOutputStream baos = new ByteArrayOutputStream();161 byte[] imageArray = {};162 try {163 ImageIO.write(bi, extension, baos);164 imageArray = baos.toByteArray();165 baos.close();166 } catch (IOException e) {167 log.error("Error while decoding the compressed BufferedImage to base64 ", e);168 }169 return imageArray;170 }171 private void writeThumbnailForSVG(File initialSVG, File thumbnailSVG) {172 String base64PNGImage = getPNGFromSVG(initialSVG);173 byte[] scaledDownInBytes = compressToThumbnail(base64PNGImage, "png");174 Dimension imageDimension = getImageDimension(scaledDownInBytes);175 String base64ScaledDownContent = BaseEncoding.base64().encode(scaledDownInBytes);176 createSVGThumbFile(thumbnailSVG, base64ScaledDownContent, imageDimension);177 }178 Dimension getImageDimension(byte[] givenImage) {179 Dimension dimension = new Dimension();180 try (ByteArrayInputStream inputStream = new ByteArrayInputStream(givenImage)) {181 BufferedImage image = ImageIO.read(inputStream);182 dimension.height = image.getHeight();183 dimension.width = image.getWidth();184 } catch (IOException e) {185 log.error("The converted png image cannot be read.");186 }187 return dimension;...

Full Screen

Full Screen

compressToThumbnail

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.report.html5;2import com.tngtech.jgiven.attachment.Attachment;3import com.tngtech.jgiven.attachment.MediaType;4import com.tngtech.jgiven.report.model.ReportModel;5import com.tngtech.jgiven.report.model.Word;6import org.apache.commons.io.IOUtils;7import org.slf4j.Logger;8import org.slf4j.LoggerFactory;9import javax.imageio.ImageIO;10import java.awt.image.BufferedImage;11import java.io.ByteArrayOutputStream;12import java.io.IOException;13import java.util.Base64;14public class Html5AttachmentGenerator {15 private static final Logger log = LoggerFactory.getLogger(Html5AttachmentGenerator.class);16 public static Attachment generateAttachment(ReportModel reportModel, Word word, BufferedImage image, String fileExtension) {17 try {18 ByteArrayOutputStream baos = new ByteArrayOutputStream();19 ImageIO.write(image, fileExtension, baos);20 baos.flush();21 byte[] imageInByte = baos.toByteArray();22 baos.close();23 Attachment attachment = new Attachment();24 attachment.setMediaType(MediaType.PNG);25 attachment.setContent(Base64.getEncoder().encodeToString(imageInByte));26 attachment.setFileName(word.getWord() + "." + fileExtension);27 attachment.setFileExtension(fileExtension);28 return attachment;29 } catch (IOException e) {30 log.error("Error while generating attachment", e);31 }32 return null;33 }34 public static Attachment generateAttachment(ReportModel reportModel, Word word, BufferedImage image) {35 return generateAttachment(reportModel, word, image, "png");36 }37 public static Attachment generateAttachment(ReportModel reportModel, Word word, byte[] image) {38 try {39 Attachment attachment = new Attachment();40 attachment.setMediaType(MediaType.PNG);41 attachment.setContent(Base64.getEncoder().encodeToString(image));42 attachment.setFileName(word.getWord() + ".png");43 attachment.setFileExtension("png");44 return attachment;45 } catch (Exception e) {46 log.error("Error while generating attachment", e);47 }48 return null;49 }50 public static Attachment generateAttachment(ReportModel reportModel, Word word, byte[] image, String fileExtension) {51 try {52 Attachment attachment = new Attachment();53 attachment.setMediaType(MediaType.PNG);54 attachment.setContent(Base64.getEncoder().encodeToString(image));55 attachment.setFileName(word.getWord() + "." + fileExtension);56 attachment.setFileExtension(fileExtension);57 return attachment;58 } catch (Exception e

Full Screen

Full Screen

compressToThumbnail

Using AI Code Generation

copy

Full Screen

1Html5AttachmentGenerator generator = new Html5AttachmentGenerator();2generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );3Html5AttachmentGenerator generator = new Html5AttachmentGenerator();4generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );5Html5AttachmentGenerator generator = new Html5AttachmentGenerator();6generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );7Html5AttachmentGenerator generator = new Html5AttachmentGenerator();8generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );9Html5AttachmentGenerator generator = new Html5AttachmentGenerator();10generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );11Html5AttachmentGenerator generator = new Html5AttachmentGenerator();12generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );13Html5AttachmentGenerator generator = new Html5AttachmentGenerator();14generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );15Html5AttachmentGenerator generator = new Html5AttachmentGenerator();16generator.compressToThumbnail( attachment, "image/png", 100, 100, 0.5f );17Html5AttachmentGenerator generator = new Html5AttachmentGenerator();18generator.compressToThumbnail( attachment, "image/png", 100,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JGiven automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful