gpt4 book ai didi

java - 使用 Java 创建图像的缩略图 View

转载 作者:行者123 更新时间:2023-11-30 09:49:19 25 4
gpt4 key购买 nike

我有一个应用程序,我需要选择一个包含图片的文件夹,并且需要使用 Java 显示这些图像的缩略图 View 。我不知道如何以缩略图格式表示图像。

代码示例、理论或链接等任何资源都非常有用。

谢谢

最佳答案

我在我的一个项目中使用了这段代码。我前段时间在网上找到的(不知道在哪里,但如果有人知道请告诉我,以便我引用):

private static byte[] createThumbnail(byte[] bytes)
{
try
{
double scale;
int sizeDifference, originalImageLargestDim;
Image inImage = ImageIO.read(new ByteArrayInputStream(bytes));

//find biggest dimension
if(inImage.getWidth(null) > inImage.getHeight(null))
{
scale = (double)LARGEST_DIMENSION/(double)inImage.getWidth(null);
sizeDifference = inImage.getWidth(null) - LARGEST_DIMENSION;
originalImageLargestDim = inImage.getWidth(null);
}
else
{
scale = (double)LARGEST_DIMENSION/(double)inImage.getHeight(null);
sizeDifference = inImage.getHeight(null) - LARGEST_DIMENSION;
originalImageLargestDim = inImage.getHeight(null);
}
//create an image buffer to draw to
BufferedImage outImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); //arbitrary init so code compiles
Graphics2D g2d;
AffineTransform tx;
if(scale < 1.0d) //only scale if desired size is smaller than original
{
int numSteps = sizeDifference / 100;
int stepSize = sizeDifference / numSteps;
int stepWeight = stepSize/2;
int heavierStepSize = stepSize + stepWeight;
int lighterStepSize = stepSize - stepWeight;
int currentStepSize, centerStep;
double scaledW = inImage.getWidth(null);
double scaledH = inImage.getHeight(null);
if(numSteps % 2 == 1) //if there's an odd number of steps
centerStep = (int)Math.ceil((double)numSteps / 2d); //find the center step
else
centerStep = -1; //set it to -1 so it's ignored later
Integer intermediateSize = originalImageLargestDim, previousIntermediateSize = originalImageLargestDim;
for(Integer i=0; i<numSteps; i++)
{
if(i+1 != centerStep) //if this isn't the center step
{
if(i == numSteps-1) //if this is the last step
{
//fix the stepsize to account for decimal place errors previously
currentStepSize = previousIntermediateSize - LARGEST_DIMENSION;
}
else
{
if(numSteps - i > numSteps/2) //if we're in the first half of the reductions
currentStepSize = heavierStepSize;
else
currentStepSize = lighterStepSize;
}
}
else //center step, use natural step size
{
currentStepSize = stepSize;
}
intermediateSize = previousIntermediateSize - currentStepSize;
scale = (double)intermediateSize/(double)previousIntermediateSize;
scaledW = (int)scaledW*scale;
scaledH = (int)scaledH*scale;
outImage = new BufferedImage((int)scaledW, (int)scaledH, BufferedImage.TYPE_INT_RGB);
g2d = outImage.createGraphics();
g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, outImage.getWidth(), outImage.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
tx = new AffineTransform();
tx.scale(scale, scale);
g2d.drawImage(inImage, tx, null);
g2d.dispose();
inImage = new ImageIcon(outImage).getImage();
previousIntermediateSize = intermediateSize;
}
}
else
{
//just copy the original
outImage = new BufferedImage(inImage.getWidth(null), inImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
g2d = outImage.createGraphics();
g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, outImage.getWidth(), outImage.getHeight());
tx = new AffineTransform();
tx.setToIdentity(); //use identity matrix so image is copied exactly
g2d.drawImage(inImage, tx, null);
g2d.dispose();
}
//JPEG-encode the image and write to file.
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(outImage);
return os.toByteArray();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}

关于java - 使用 Java 创建图像的缩略图 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5848029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com