本文整理汇总了Java中org.imgscalr.Scalr.crop方法的典型用法代码示例。如果您正苦于以下问题:Java Scalr.crop方法的具体用法?Java Scalr.crop怎么用?Java Scalr.crop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.imgscalr.Scalr
的用法示例。
在下文中一共展示了Scalr.crop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadThumbnail
import org.imgscalr.Scalr; //导入方法依赖的package包/类
public void uploadThumbnail(String path, ByteArrayOutputStream buffer, int width, int height) throws IOException {
ByteArrayOutputStream thumbBuffer = new ByteArrayOutputStream();
BufferedImage thumb = ImageIO.read(new ByteArrayInputStream(buffer.toByteArray()));
thumb = Scalr.resize(thumb, Method.ULTRA_QUALITY,
thumb.getHeight() < thumb.getWidth() ? Mode.FIT_TO_HEIGHT : Mode.FIT_TO_WIDTH,
Math.max(width, height), Math.max(width, height), Scalr.OP_ANTIALIAS);
thumb = Scalr.crop(thumb, width, height);
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // Needed see javadoc
param.setCompressionQuality(1.0F); // Highest quality
writer.setOutput(ImageIO.createImageOutputStream(thumbBuffer));
writer.write(thumb);
if (path.lastIndexOf('.') != -1) {
path = path.substring(0, path.lastIndexOf('.'));
}
super.put(path + "." + width + "x" + height + ".jpg", new ByteArrayInputStream(thumbBuffer.toByteArray()),
Long.valueOf(thumbBuffer.size()));
}
示例2: createThumbnail
import org.imgscalr.Scalr; //导入方法依赖的package包/类
/**
* creates the Thumbnail for the given picture and stores it on the disk
*
* @param image
*/
public static void createThumbnail(File image, int size, File dest) throws IOException {
BufferedImage thumbnail = ImageIO.read(image);
int width = thumbnail.getWidth();
int height = thumbnail.getHeight();
if (width > size || height > size) {
if (width > height) {
thumbnail = Scalr.resize(thumbnail, Scalr.Mode.FIT_TO_HEIGHT, size);
thumbnail = Scalr.crop(thumbnail, thumbnail.getWidth() / 2 - size / 2, 0, size, size);
} else {
thumbnail = Scalr.resize(thumbnail, Scalr.Mode.FIT_TO_WIDTH, size);
thumbnail = Scalr.crop(thumbnail, 0, thumbnail.getWidth() / 2 - size / 2, size, size);
}
}
String suffix;
if (image.getName().lastIndexOf(".") != -1) {
suffix = image.getName().substring(image.getName().lastIndexOf(".") + 1);
} else {
suffix = "png";//by default.
}
ImageIO.write(thumbnail, suffix, dest);
}
示例3: createThumbnail
import org.imgscalr.Scalr; //导入方法依赖的package包/类
public static byte[] createThumbnail(InputStream in, int size) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage thumbnail = ImageIO.read(in);
int width = thumbnail.getWidth();
int height = thumbnail.getHeight();
if (width > size || height > size) {
if (width > height) {
thumbnail = Scalr.resize(thumbnail, Scalr.Mode.FIT_TO_HEIGHT, size);
thumbnail = Scalr.crop(thumbnail, thumbnail.getWidth() / 2 - size / 2, 0, size, size);
} else {
thumbnail = Scalr.resize(thumbnail, Scalr.Mode.FIT_TO_WIDTH, size);
thumbnail = Scalr.crop(thumbnail, 0, thumbnail.getWidth() / 2 - size / 2, size, size);
}
}
try {
ImageIO.write(thumbnail, "jpeg", baos);
baos.flush();
return baos.toByteArray();
} finally {
baos.close();
}
}
示例4: toRatio
import org.imgscalr.Scalr; //导入方法依赖的package包/类
/**
* Returns a new image scaled and cropped to the specific width/height ratio
*
* @param image
* @param ratio
* @return
*/
private BufferedImage toRatio(BufferedImage image, float ratio)
{
if (ratio == 0f)
return image;
int width = image.getWidth();
int height = image.getHeight();
float oldRatio = (float) width / (float) height;
if (ratio < oldRatio) // Original is wider
{
int newWidth = Math.round((float) height * ratio);
int x = Math.round((float) (width - newWidth) / 2f); // Crop away left and right edges
return Scalr.crop(image, x, 0, newWidth, height);
} else
{ // Original is taller
int newHeight = Math.round((float) width / ratio);
int y = Math.round((float) (height - newHeight) / 2f); // Crop away top and bottom edges
return Scalr.crop(image, 0, y, width, newHeight);
}
}
示例5: run
import org.imgscalr.Scalr; //导入方法依赖的package包/类
@Override
public void run()
{
try
{
if(width > 0 && height > 0)
{
BufferedImage resizedImage = Scalr.resize(image, Scalr.Method.BALANCED, Scalr.Mode.FIT_TO_WIDTH, width, height, Scalr.OP_ANTIALIAS);
if (resizedImage.getHeight() > height)
{
resizedImage = Scalr.crop(resizedImage, resizedImage.getWidth(), height);
}
ImageIO.write(resizedImage, "PNG", new File(path));
}
else
{
ImageIO.write(image, "PNG", new File(path));
}
}
catch (Exception e)
{
LOGGER.error("Unable to save screenshot: " + e.getMessage());
}
}
示例6: resizeAndCropCenter
import org.imgscalr.Scalr; //导入方法依赖的package包/类
private static BufferedImage resizeAndCropCenter(final BufferedImage image, final int width, final int height) {
final boolean verticalCrop = ((double) image.getWidth()) / ((double) image.getHeight()) < ((double) width) / ((double) height);
final Scalr.Mode mode = verticalCrop ? Scalr.Mode.FIT_TO_WIDTH : Scalr.Mode.FIT_TO_HEIGHT;
final BufferedImage resized = Scalr.resize(image, QUALITY_SETTING, mode, width, height);
final int dx = verticalCrop ? 0 : (resized.getWidth() - width) / 2;
final int dy = !verticalCrop ? 0 : (resized.getHeight() - height) / 2;
return Scalr.crop(resized, dx, dy, width, height);
}
示例7: crop
import org.imgscalr.Scalr; //导入方法依赖的package包/类
/**
* Crop the image if the request contains cropping parameters. Fail if the crop parameters are invalid or incomplete.
*
* @param originalImage
* @param cropArea
* @return cropped image or return original image if no cropping is requested
*/
protected BufferedImage crop(BufferedImage originalImage, ImageRect cropArea) {
if (cropArea != null) {
cropArea.validateCropBounds(originalImage.getWidth(), originalImage.getHeight());
try {
BufferedImage image = Scalr.crop(originalImage, cropArea.getStartX(), cropArea.getStartY(), cropArea.getWidth(),
cropArea.getHeight());
originalImage.flush();
return image;
} catch (IllegalArgumentException e) {
throw error(BAD_REQUEST, "image_error_cropping_failed", e);
}
}
return originalImage;
}
示例8: cropImage
import org.imgscalr.Scalr; //导入方法依赖的package包/类
/**
* Return the image which resized(support JPG, PNG, GIF)
* @param image The BufferedImage which based on InputStream
* BufferedImage image = ImageIO.read(InputStream)
* @param newWidth Max width of thumbnail will be resized
* @param newHeight Max height of thumbnail will be resized
* @return InputStream
* @throws Exception
*/
public static void cropImage(File i, int newWidth, int newHeight) throws Exception {
BufferedImage image = ImageIO.read(i);
// first get the width and the height of the image
int originWidth = image.getWidth();
int originHeight = image.getHeight();
if (originWidth <= newWidth && originHeight <= newHeight) {
return;
}
// Make sure the aspect ratio is maintained, so the image is not skewed
BufferedImage thumbImage = Scalr.crop(image, newWidth, newHeight);
String suffix = i.getName().substring(i.getName().lastIndexOf(".") + 1);
ImageIO.write(thumbImage, suffix, i);
}
示例9: processImage
import org.imgscalr.Scalr; //导入方法依赖的package包/类
private BufferedImage processImage(UploadedFile avatar) throws IOException {
BufferedImage image = ImageIO.read(avatar.getFile());
if (image == null) {
throw new IOException();
}
int min = Math.min(image.getHeight(), image.getWidth());
BufferedImage cropped = Scalr.crop(image, min, min);
return Scalr.resize(cropped, Scalr.Method.ULTRA_QUALITY, 150);
}
示例10: doResize
import org.imgscalr.Scalr; //导入方法依赖的package包/类
private BufferedImage doResize(Integer width, Integer height, boolean stretch,
BufferedImage srcImg) {
BufferedImage resized = null;
if (width != null && height != null && !stretch &&
(allowImageEnlargement || (width < srcImg.getWidth() && height < srcImg.getHeight()))) {
if (srcImg.getHeight()/(float)height < srcImg.getWidth()/(float)width) {
resized = Scalr.resize(srcImg, Method.ULTRA_QUALITY,
Mode.FIT_TO_HEIGHT, width, height);
} else {
resized = Scalr.resize(srcImg, Method.ULTRA_QUALITY,
Mode.FIT_TO_WIDTH, width, height);
}
resized.flush();
int x = (resized.getWidth() - width) / 2;
int y = (resized.getHeight() - height) / 2;
resized = Scalr.crop(resized, x, y, width, height);
} else if (width != null && height != null &&
(allowImageEnlargement || (width < srcImg.getWidth() && height < srcImg.getHeight()))) {
resized = Scalr.resize(srcImg, Method.ULTRA_QUALITY,
Mode.FIT_EXACT, width, height);
} else if (height != null && (allowImageEnlargement || height < srcImg.getHeight())) {
resized = Scalr.resize(srcImg, Method.ULTRA_QUALITY,
Mode.FIT_TO_HEIGHT, height);
} else if (width != null && (allowImageEnlargement || width < srcImg.getWidth())) {
resized = Scalr.resize(srcImg, Method.ULTRA_QUALITY,
Mode.FIT_TO_WIDTH, width);
}
if (resized == null) {
resized = srcImg;
}
return resized;
}
示例11: crop
import org.imgscalr.Scalr; //导入方法依赖的package包/类
static public void crop(File file, int x, int y, int width, int height) throws FileOperationException {
BufferedImage image;
try {
image = ImageIO.read(file);
image = Scalr.crop(image, x, y, width, height);
saveToJPG(image, file);
image.flush();
} catch (IOException | IllegalArgumentException e){
logger.error(e.getMessage(), e);
throw new FileOperationException("Cropping failed");
}
}
示例12: handle
import org.imgscalr.Scalr; //导入方法依赖的package包/类
@Override
protected BufferedImage handle(BufferedImage bufferedImage) {
// CropFilter filter = new CropFilter(left, top, width, height);
// return filter.filter(bufferedImage, null);
BufferedImage croped = Scalr.crop(bufferedImage,
left, top, width, height, Scalr.OP_ANTIALIAS);
return croped;
}
示例13: handle
import org.imgscalr.Scalr; //导入方法依赖的package包/类
@Override
protected BufferedImage handle(BufferedImage bufferedImage) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (width == height){
return bufferedImage;
}
int left = 0;
int top = 0;
if (width > height){
left = (width - height) / 2;
width = height;
}else {
top = (height - width) / 2;
height = width;
}
BufferedImage croped = Scalr.crop(bufferedImage,
left, top, width, height, Scalr.OP_ANTIALIAS);
// CropFilter filter = new CropFilter(left, top, width, height);
// return filter.filter(bufferedImage, null);
return croped;
}
示例14: resizeImg
import org.imgscalr.Scalr; //导入方法依赖的package包/类
/**
* Resizes image according to specified dimensions.
*
* @param bufImage
* - image to resize.
* @param width
* - new image width.
* @param height
* - new image height.
* @param path
* - path to screenshot file.
*/
private static void resizeImg(BufferedImage bufImage, int width, int height, String path) {
try {
bufImage = Scalr.resize(bufImage, Scalr.Method.BALANCED, Scalr.Mode.FIT_TO_WIDTH, width, height,
Scalr.OP_ANTIALIAS);
if (bufImage.getHeight() > height) {
bufImage = Scalr.crop(bufImage, bufImage.getWidth(), height);
}
ImageIO.write(bufImage, "png", new File(path));
} catch (Exception e) {
LOGGER.error("Image scaling problem!");
}
}
示例15: intResizeImage
import org.imgscalr.Scalr; //导入方法依赖的package包/类
private BufferedImage intResizeImage(java.awt.Image inImg, int sizeW, int sizeH, boolean crop) {
// logger.info("CROP: "+crop);
final int
w_size = inImg.getWidth(null),
h_size = inImg.getHeight(null);
final BufferedImage image = new BufferedImage(w_size, h_size, BufferedImage.TYPE_INT_RGB);
final Graphics2D g2d = image.createGraphics();
g2d.setBackground(new Color(255, 255, 255));
g2d.setColor(new Color(255, 255, 255));
g2d.fillRect(0, 0, w_size, h_size);
g2d.drawImage(inImg, 0, 0, null);
g2d.dispose();
final BufferedImage out_img, crop_img;
if (crop) {
if (w_size > h_size) {
final double
in_k = ((double) w_size) / ((double) h_size),
out_k = ((double) sizeW) / ((double) sizeH);
out_img = Scalr.resize(image, quality, Scalr.Mode.FIT_TO_HEIGHT, ((in_k < out_k) ? ((int)(sizeH * out_k)) : (sizeH)), Scalr.OP_ANTIALIAS);
final int
out_x = (out_img.getWidth() - sizeW) / 2,
out_y = (out_img.getHeight() - sizeH) / 2;
crop_img = Scalr.crop(out_img, out_x, out_y, sizeW, sizeH, Scalr.OP_ANTIALIAS);
} else {
final double
in_k = ((double) h_size) / ((double) w_size),
out_k = ((double) sizeH) / ((double) sizeW);
out_img = Scalr.resize(image, quality, Scalr.Mode.FIT_TO_WIDTH, ((in_k < out_k) ? ((int)(sizeW * out_k)) : (sizeW)), Scalr.OP_ANTIALIAS);
final int
out_x = (out_img.getWidth() - sizeW) / 2,
out_y = (out_img.getHeight() - sizeH) / 2;
crop_img = Scalr.crop(out_img, out_x, out_y, sizeW, sizeH, Scalr.OP_ANTIALIAS);
}
return crop_img;
} else {
out_img = Scalr.resize(image, quality, Scalr.Mode.AUTOMATIC, sizeW, sizeH, Scalr.OP_ANTIALIAS);
return out_img;
}
}