我正在開發一個應用程序隱藏文本使用隱寫方法稱爲LSB,將其放入圖像。但是在測試過程中,我發現當你在圖庫中保存一個圖像,然後從那裏加載圖像時,它的RGB值發生了變化。這是紅色值:Swift 3 - 保存圖像更改RGB值
34 -> 41
29 -> 34
44 -> 46
63 -> 62
101 -> 105
118 -> 119
左 - 他們是什麼,對 - 他們成了什麼。當然,這樣的改變完全破壞了隱藏在裏面的文字。這是我用來保存圖像的代碼:
UIImageWriteToSavedPhotosAlbum(newImg, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
let ac = UIAlertController(title: "Saving error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Saved to the gallery", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
這是我提取RGB值的方式:
func pixelData(image: UIImage) -> [UInt8]? {
let size = image.size
let dataSize = size.width * size.height * 4
var pixelData = [UInt8](repeating: 0, count: Int(dataSize))
let colorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGContext(data: &pixelData,width: Int(size.width), height: Int(size.height), bitsPerComponent: 8, bytesPerRow: 4 * Int(size.width), space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)
guard let cgImage = image.cgImage else { return nil }
context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
return pixelData
}
我需要一種方法來保存圖像,正是因爲它是。任何幫助?
編輯1.文字圖像注射FUNC:
func encrypt(image: UIImage, message: String) -> UIImage {
var text = message
text += endSymbols //Need to decrypt message
var RGBArray = pixelData(image: image)!
let binaryMessage = toBinary(string: text) //Convert characters to binary ASCII number
var counter: Int = 0
for letter in binaryMessage {
for char in letter.characters {
let num = RGBArray[counter]
let characterBit = char
let bitValue = UInt8(String(characterBit))
let resultNum = (num & 0b11111110) | bitValue!
RGBArray[counter] = resultNum
counter += 4 //Modify only RED values bits
}
}
let resultImg = toImage(data: RGBArray, width: Int(image.size.width), height: Int(image.size.height))
return resultImg!
}
當您保存圖像時,它是否爲JPEG格式? – Reti43
是的,我在我的.jpg照片和模擬器上的圖庫上測試了Apple的默認圖像 – askrav
因爲圖像格式有損(可以在存儲過程中修改像素以實現壓縮),所以不能使用LSB替換的jpg照片。使用bmp,png或其他任何無損格式。 – Reti43