образца, основанный на этом [вывешены] (Storing message into R,G,B instead of Alpha)Запрошенный размер массива превышает VM предел
На этот раз я хотел бы только RGB вместо ARGB, но на этот раз я получил длину байт 2147483647. Ниже приведены часть кода, где я изменился.
входной только 128 байтов массива.
EmbedMessage
private void openImage() {
File f = new File("C:/TEMP/CROP.png");
try {
sourceImage = ImageIO.read(f);
sourceImage = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = sourceImage.createGraphics();
g.drawImage(ImageIO.read(f), 0, 0, null);
g.dispose();
this.embedMessage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private void embedInteger(BufferedImage img, int n, int start) {
int maxX = img.getWidth(), maxY = img.getHeight(),
startX = start/maxY, startY = start - startX*maxY, count=0;
for(int i=startX; i<maxX && count<32; i++) {
for(int j=startY; j<maxY && count<32; j++) {
int rgb = img.getRGB(i, j);
// bit = getBitValue(n, count);
//rgb = setBitValue(rgb, 0, bit);
int bit = getBitValue(n, count); rgb = setBitValue(rgb, 0, bit);
bit = getBitValue(n, count+1); rgb = setBitValue(rgb, 8, bit);
bit = getBitValue(n, count+2); rgb = setBitValue(rgb, 16, bit);
img.setRGB(i, j, rgb);
count = count+3;
}
}
}
private void embedByte(BufferedImage img, byte b, int start) {
int maxX = img.getWidth(), maxY = img.getHeight(),
startX = start/maxY, startY = start - startX*maxY, count=0;
for(int i=startX; i<maxX && count<8; i++) {
for(int j=startY; j<maxY && count<8; j++) {
if(j==maxY-1) {
startY = 0;
}
int rgb = img.getRGB(i, j);
//bit = getBitValue(b, count);
// rgb = setBitValue(rgb, 0, bit);
int bit = getBitValue(b, count); rgb = setBitValue(rgb, 0, bit);
bit = getBitValue(b, count+1); rgb = setBitValue(rgb, 8, bit);
bit = getBitValue(b, count+2); rgb = setBitValue(rgb, 16, bit);
img.setRGB(i, j, rgb);
count = count+3;
}
}
}
DecodeMessage
private void openImage() throws Exception {
File f = new File("C:/TEMP/Four Area/Crop image/chest-CROP2.png");
//File f = new File("C:/TEMP/chest2.png");
try {
image = ImageIO.read(f);
image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(ImageIO.read(f), 0, 0, null);
g.dispose();
this.decodeMessage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private int extractInteger(BufferedImage img, int start) {
int maxX = img.getWidth(), maxY = img.getHeight(),
startX = start/maxY, startY = start - startX*maxY, count=0;
int length = 0;
for(int i=startX; i<maxX && count<32; i++) {
for(int j=startY; j<maxY && count<32; j++) {
int rgb = img.getRGB(i, j);
//bit = getBitValue(rgb, 0);
//length = setBitValue(length, count, bit);
int bit = getBitValue(rgb, 0); length = setBitValue(length, count, bit);
bit = getBitValue(rgb, 8); length = setBitValue(length, count+1, bit);
bit = getBitValue(rgb, 16); length = setBitValue(length, count+2, bit);
count = count+3;
}
}
return length;
}
private byte extractByte(BufferedImage img, int start) {
int maxX = img.getWidth(), maxY = img.getHeight(),
startX = start/maxY, startY = start - startX*maxY, count=0;
byte b = 0;
for(int i=startX; i<maxX && count<8; i++) {
for(int j=startY; j<maxY && count<8; j++) {
if(j==maxY-1) {
startY = 0;
}
int rgb = img.getRGB(i, j);
//bit = getBitValue(rgb, 0);
//b = (byte)setBitValue(b, count, bit);
int bit = getBitValue(rgb, 0); b = (byte)setBitValue(b, count, bit);
bit = getBitValue(rgb, 8); b = (byte)setBitValue(b, count+1, bit);
bit = getBitValue(rgb, 16); b = (byte)setBitValue(b, count+2, bit);
count = count+3;
}
}
return b;
}
В 'embedMessage', (i * 8 + 32)/3, номер 3 здесь, поскольку компоненты R, G, B 3? но почему мы не делаем этого, когда делаем ARGB (например, do (i * 8 + 32)/4) –
(i * 8 + 32)/4 = i * 2 + 8. Просто второе требует меньше вычислений. – Reti43
Извините, у меня есть два последних вопроса о коде. 1) почему сообщение нужно 32 бит для первого, почему бы не 24 бита (потому что я думал, что 32-битный, потому что 4 компонента, или потому, что int?). 2) зачем делать 'count <= 29' в' embedInteger' и почему 29, то же самое, что 'embedByte', почему' count <= 5' и почему 5 ?? –