I am embedding pdf into an Image. But after extraction i am getting blank page pdf. How to extract the correct pdf file whatever i have inserted?
4 次查看(过去 30 天)
显示 更早的评论
% Embedding PDF file into an image using LSB substitution
% Set the file names for the PDF file and the cover image
pdfFileName = 'UMNwriteup.pdf';
imageFileName = 'glioma.jpg';
% Read the PDF file as binary
pdfData = fileread(pdfFileName);
pdfData = uint8(pdfData);
% Read the cover image
coverImage = imread(imageFileName);
% Get the dimensions of the cover image
[rows, columns, ~] = size(coverImage);
% Calculate the maximum number of bytes that can be embedded
maxBytes = (rows * columns * 3) / 8;
% Check if the PDF file size exceeds the maximum embedding capacity
if numel(pdfData) > maxBytes
error('PDF file size exceeds the maximum embedding capacity of the cover image.');
end
% Convert the PDF data into binary format
pdfBinary = de2bi(pdfData, 8, 'left-msb');
pdfBinary = pdfBinary(:);
% Get the number of bits to be embedded
numBits = numel(pdfBinary);
% Reshape the cover image to match the number of bits
coverImage = reshape(coverImage, [], 1);
% Embed the PDF data into the cover image using LSB substitution
coverImage(1:numBits) = bitset(coverImage(1:numBits), 1, pdfBinary);
% Reshape the modified cover image back to the original dimensions
coverImage = reshape(coverImage, rows, columns, 3);
% Save the stego image with the embedded PDF data
stegoImageFileName = 'stego_image.png';
imwrite(coverImage, stegoImageFileName);
% Extraction of PDF file from the stego image
% Read the stego image
stegoImage = imread(stegoImageFileName);
% Reshape the stego image into a single column
stegoImage = reshape(stegoImage, [], 1);
% Extract the embedded PDF data from the stego image
extractedPDFBinary = bitget(stegoImage(1:numBits), 1);
% Reshape the extracted binary data into bytes
extractedPDFData = reshape(extractedPDFBinary, [], 8);
extractedPDFData = bi2de(extractedPDFData, 'left-msb');
% Convert the extracted PDF data from uint8 to char
extractedPDFData = char(extractedPDFData);
% Write the extracted PDF data to a file
outputFileName = 'extracted.pdf';
fid = fopen(outputFileName, 'w');
fwrite(fid, extractedPDFData, 'uint8');
fclose(fid);
disp('Extraction complete.');
disp(['The extracted PDF file has been saved as: ' outputFileName]);
0 个评论
采纳的回答
Image Analyst
2023-7-3
See my attached stego/hiding/watermarking demos. Maybe there is something there that you can use or adapt. Good luck.
更多回答(1 个)
DGM
2023-7-2
编辑:DGM
2023-7-2
fileread() is really just a convenience wrapper for fread() meant for reading text files. Up until R2020-something, it didn't even have a means to even specify the encoding. It just blindly read the file using a default presumed encoding. In this case, it's likely reading the data using a two-byte encoding, so casting the char vector as uint8 destroys the data.
If you want to read a binary file strictly bytewise, just use fread(...,'*uint8') instead of trying to work around the automatic encoding detection used by fileread() or by fread(...,'*char')
% Read the PDF file as binary
fid = fopen(pdfFileName,'r');
pdfData = fread(fid,'*uint8');
fclose(fid);
See the bottom of the table here for the comments on how char inputs are handled:
3 个评论
DGM
2023-7-3
I don't know. I don't know where that jar file comes from, but I can't run that on my installation.
Using fileread() like that with the PDFs I've tested on my system in R2019b will reliably result in the read data being corrupted (values are different, wrong number of bytes returned). A char is not necessarily 1 byte.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!