I'd like to ask you a question.
Existing code
byte[] photo;
if(upload.isEmpty() == false){
logger.info("-------------Upload file start -------------");
logger.info("name : "+upload.getName());
logger.info("filename : "+upload.getOriginalFilename());
logger.info("size : "+upload.getSize());
logger.info("-------------- file end --------------\n");
photo = upload.getBytes();
upload.getSize();
}else{
logger.info("-------------Camera file start -------------");
logger.info("name : "+camera.getName());
logger.info("filename : "+camera.getOriginalFilename());
logger.info("size : "+camera.getSize());
logger.info("-------------- file end --------------\n");
photo = camera.getBytes();
camera.getSize();
}
byte[] data = photo;
String imgName = "idCard"+ "_" + param.get("sp_no") +".jpg";
String imgPath = "C:/test/";
param.put("imgName", imgName);
FileOutputStream imageout = new FileOutputStream(imgPath + imgName);
imageout.write(data);
imageout.flush();
imageout.close();
return "photo/idCard";
It's a code that stores byte-type images in this way. I want to resize byte-type photo images, so I'm going to make a class and then resize them. The code I used is to receive and save images in String-type absolute path, but I want to store byte-type images on the server.
Stringorg_img = "d://IMG_22.jpg">;//Original file path
String thumb_img = "d://thumb.jpg";//path to create thumbnail
File src = new File(org_img);
File dest = new File(thumb_img);
ImageUtill.resize(src,dest,400,300);
It is a method of resizing an image in the form of a String using ImageUtil class, but it is difficult to apply the form received in the form of a byte like the code above, so please advise how to do it.
java spring
If you look at the resize of ImageUtil, you eventually get Image srcImg
from the src file.
The image in the form of byte[] can be converted into an image, so you can modify the resize method a little bit and use it.
byte[] imageInByte;
Image image;
try {
// // convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
image = ImageIO.read(in);
} } catch (IOException e) {
System.out.println(e.getMessage());
}
package com.wein.wincos.cc;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;//Relevant to file input/output
import java.io.IOException;//Handle input/output exceptions
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageUtill {
//Constant treatment as final
public static final int RATIO = 0;
public static final int SAME=-1;
public static void resize(File src, File dest, int width, int height)
THROWS IOException{// Keep in mind that this is a method for thumbnail images!!
//src:image path dest:thumbnail path width:horizontal, height:vertical
Image srcImg = null;//class objectification
String suffix = src.getName().substring(src.getName().lastIndexOf('.')+1).toLowerCase();
//suffix variable: Get the name of the src path and make it into characters by using the index +1 of . Find the name without the extension.
if(suffix.equals("bmp") || suffix.equals("png") || suffix.equals("gif")){
srcImg = ImageIO.read(src);
//Set each extension and read the file if it is.
}else{
srcImg = new ImageIcon(src.getAbsolutePath()).getImage();
//This guy is jpg only (because the ImageIcon method itself only loads jpg...)
}
//The corresponding image size
int srcWidth = srcImg.getWidth(null);
int srcHeight = srcImg.getHeight(null);
// corresponding thumbnail image (none)
int destWidth=-1, destHeight=-1;
if(width==SAME){//If the width is 0
destWidth=srcWidth;//Thumbnail image is equal to the image size.
}else if(width>0){
If it is greater than destWidth=width;//0, it has a thumbnail image.
}
if(height==SAME){//If the length is zero
The destHeight = srcHeight;//thumbnail image is the same as the image size.
}else if(height>0){
If it is greater than destHeight=height;//0, it has a thumbnail image.
}
If (width==RATIO &&height==RATIO){//The aspect ratio is -1
destWidth=srcWidth;
destHeight=srcHeight;
}else if(width==RATIO){
//Set a constant aspect ratio.
double ratio = ((double)destHeight)/((double)srcHeight);
destWidth=(int)((double)destHeight*ratio);
}else if(height==RATIO){
double ratio = ((double)destWidth)/((double)srcWidth);
destHeight=(int)((double)destHeight*ratio);
}
Image imgTarget = srcImg.getScaledInstance(destWidth, destHeight, Image.SCALE_SMOOTH);
//It allows the image to be reduced smoothly without breaking.
int pixels[] = new int[destWidth * destHeight];
//Calculate the full pixel value.
PixelGrabber pg
= = new PixelGrabber(imgTarget, 0, 0, destWidth, destHeight, pixels, 0, destWidth);
//It naturally reduces the image according to the size.
try {
pg.grabPixels();
//Run Shrink Image
} } catch (Exception e) {
throw new IOException(e.getMessage());
}
BufferedImage destImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
destImg.setRGB(0, 0, destWidth, destHeight, pixels, 0, destWidth);
//The image you read
ImageIO.write(destImg, "jpg", dest);
//Thumbnail image.Create a file in jpg format.
}
}
ImageUtil class. It's a resizing method that you use a lot, and most of them use String method to receive and use images through the path, but I want to save images in the form of bye like me.
© 2024 OneMinuteCode. All rights reserved.