我有一个1便士高和宽的巴菲特酒窝。我需要创建另一个巴菲特酒窝,将在1:1的分辨率。我知道在这种情况下,通过计算宽度的平方根可以做些什么,但是如果结果不是整数呢?我有一个从宽度和高度创建矩阵的方法(每个像素的列表),所以不会有问题。最后,如果有空像素,我可以使它们透明,所以不一定是一个完全填满的方块。我只需要得到正方形的尺寸,有宽度的原始照片,我会自己填写像素。
我的代码:
public static BufferedImage encode(String str){
byte[] bytes = str.getBytes();
BufferedImage img = new BufferedImage(bytes.length, 1, BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i < bytes.length; i++) {
img.setRGB(i, 0,encode(bytes[i]).getRGB() );
}
return img;
}
public static Color encode(byte byt){
return new Color(byt+128,byt+128,byt+128);
}发布于 2022-11-17 15:31:29
public static BufferedImage encode(String str){
byte[] bytes = str.getBytes();
int w = bytes.length;
int width = (int) Math.ceil(Math.sqrt(w));
BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
for (int index = 0; index < bytes.length; index++) {
img.setRGB(index%width, (int)Math.floor(index/(float)width),encode(bytes[index]).getRGB() );
}
return img;
}代码更新。我只计算平方根,如果这个数字不是一个整数,那么我把它算成一个更大的整数,结果得到图像的高度,因为图像有1:1的格式,我们立即得到了宽度。然后我只需填写公式:
x = byteIndex % imageWidth
y = floor(byteIndex / imageHeight)https://stackoverflow.com/questions/74476777
复制相似问题