Created
May 16, 2018 16:00
-
-
Save pagetronic/ac55ebedb3bee336115f19668eac182d to your computer and use it in GitHub Desktop.
Resize and crop image in base Java ImageIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package live.page.base.blobstore.utils; | |
import com.mongodb.client.MongoCursor; | |
import com.mongodb.client.model.Filters; | |
import com.mongodb.client.model.Sorts; | |
import live.page.base.db.Db; | |
import live.page.base.utils.Json; | |
import live.page.base.utils.Settings; | |
import org.bson.conversions.Bson; | |
import org.bson.types.Binary; | |
import javax.imageio.IIOImage; | |
import javax.imageio.ImageIO; | |
import javax.imageio.ImageWriteParam; | |
import javax.imageio.ImageWriter; | |
import javax.imageio.stream.ImageInputStream; | |
import javax.imageio.stream.ImageOutputStream; | |
import java.awt.Graphics2D; | |
import java.awt.Image; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
public class Thumbnailer { | |
public static Json makeThumbCache(String db_id, String type, int width, int height) throws Exception { | |
Bson filter = Filters.eq("f", db_id); | |
Bson sort = Sorts.ascending("o"); | |
MongoCursor<Json> chunks = Db.getDb("BlobChunks").find(filter).sort(sort).iterator(); | |
File tmp = File.createTempFile("pagebase_thumb", ".file"); | |
try { | |
if (!chunks.hasNext()) { | |
return null; | |
} | |
FileOutputStream outStream = new FileOutputStream(tmp); | |
while (chunks.hasNext()) { | |
outStream.write(chunks.next().getBinary("b").getData()); | |
} | |
outStream.close(); | |
Json cache = makeThumbCache(tmp, db_id, type, width, height); | |
return cache; | |
} catch (Exception e) { | |
return null; | |
} finally { | |
chunks.close(); | |
tmp.delete(); | |
} | |
} | |
public static Json makeThumbCache(File file, String file_id, String type, int width, int height) throws Exception { | |
if (!file.exists()) { | |
return null; | |
} | |
try { | |
ImageInputStream iis = ImageIO.createImageInputStream(file); | |
String format = ImageIO.getImageReaders(iis).next().getFormatName().toLowerCase(); | |
iis.close(); | |
BufferedImage image = ImageIO.read(file); | |
Json cache = new Json(); | |
String contentType; | |
if (format.equalsIgnoreCase("png")) { | |
contentType = "image/png"; | |
} else if (format.equalsIgnoreCase("jpeg") || format.equalsIgnoreCase("jpg")) { | |
contentType = "image/jpeg"; | |
} else { | |
return null; | |
} | |
String outputformat = format; | |
if (type == null) { | |
outputformat = format; | |
} else if (type.equals(".jpg")) { | |
cache.put("format", "jpg"); | |
outputformat = "jpg"; | |
} else if (type.equals(".png")) { | |
cache.put("format", "png"); | |
outputformat = "png"; | |
} | |
if (height > 0) { | |
int tempheight = (image.getHeight() * width) / image.getWidth(); | |
int tempwidth = width; | |
if (tempheight < height) { | |
tempwidth = (image.getWidth() * height) / image.getHeight(); | |
tempheight = height; | |
} | |
image = resize(image, tempwidth, tempheight, outputformat); | |
if (height > 0) { | |
image = image.getSubimage((tempwidth - width) / 2, (tempheight - height) / 2, width, height); | |
} | |
} else { | |
int tempheight = (image.getHeight() * width) / image.getWidth(); | |
int tempwidth = width; | |
image = resize(image, tempwidth, tempheight, outputformat); | |
} | |
ImageWriter writer = ImageIO.getImageWritersByFormatName(outputformat).next(); | |
ImageWriteParam param = writer.getDefaultWriteParam();// Needed see javadoc | |
if (outputformat.equals("jpeg") || outputformat.equals("jpg")) { | |
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); | |
param.setCompressionQuality(0.7F); | |
} | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ImageOutputStream ios = ImageIO.createImageOutputStream(baos); | |
writer.setOutput(ios); | |
writer.write(null, new IIOImage(image, null, null), param); | |
baos.flush(); | |
byte[] bt = baos.toByteArray(); | |
ios.close(); | |
baos.close(); | |
cache.put("blob", file_id).put("width", width).put("height", height).put("type", contentType).put("size", bt.length).put("date", new Date()); | |
if (outputformat.equalsIgnoreCase("png")) { | |
cache.put("type", "image/png"); | |
} else if (outputformat.equalsIgnoreCase("jpeg") || outputformat.equalsIgnoreCase("jpg")) { | |
cache.put("type", "image/jpeg"); | |
} | |
List<Binary> binaries = new ArrayList<>(); | |
ByteArrayInputStream bin = new ByteArrayInputStream(bt); | |
byte[] buffer = new byte[Math.min(bt.length, Settings.CHUNCK_SIZE)]; | |
while (bin.read(buffer) != -1) { | |
binaries.add(new Binary(buffer)); | |
} | |
bin.close(); | |
cache.put("binaries", binaries); | |
Db.save("BlobCache", cache); | |
return cache; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static BufferedImage resize(BufferedImage img, int width, int height, String outputformat) { | |
BufferedImage resized = new BufferedImage(width, height, | |
outputformat.equals("jpg") || outputformat.equals("jpeg") ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB | |
); | |
Graphics2D g2d = resized.createGraphics(); | |
g2d.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); | |
g2d.dispose(); | |
return resized; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment