package com.example.messagehiden;
import java.io.File;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class StegTool {
File input;
Bitmap img;
int wigth;
int height;
int x;
int y;
public StegTool(String inputName) throws IOException {
Bitmap img1 = BitmapFactory.decodeFile(inputName);
img = img1.copy(Bitmap.Config.ARGB_8888, true);
//得到图片的大小
wigth = img.getWidth();
height = img.getHeight();
x = 0;
y = 0;
}
//写入信息
public int write(String words) throws IOException{
int bit = 0;
int temp;
char[] c = words.toCharArray();
for(int i = 0; i < words.length(); i++){
for(int j = 0; j < 16; j++){//写入一个字符
temp= readIm();//从buf里面读取一个像素
bit = (c[i] >> (15-j)) & 0x0001;
temp = bit + (temp & 0xfffffffe);//修改像素
writeIm(temp);//将修改后的数据写入
if((x == img.getWidth()-1) && (y == img.getHeight()-1)){
return 0;
}else{
move();
}
}
}
return 1;
}
//读取信息
public String read(){
String words = new String("");
int temp;
char c = ' ';
do{
words += c;
c = 0;
for(int i = 0; i < 16; i++){
c *= 2;
temp = readIm() % 2;
if(temp != 0){//temp可能是-1
temp = 1;
}
c += temp;
if((x == img.getWidth()-1) && (y == img.getHeight()-1)){
return "该图片没有隐写信息";
}else{
move();
}
}
}while (c != '$');
return words;
}
private void move(){//移动到下一个位置
if((x == img.getWidth()-1) && (y < img.getHeight()-1)){
x = 0;
y++;
}else{
x++;
}
}
public void writeIm(int temp){
img.setPixel(x, y, temp);
}
private int readIm(){
return img.getPixel(x, y);
}
}
这段代码可以把一段输入的string存储到图片中,但是只能将TXT文档的路径存入图片,一个JAVA基础薄弱的非计算机专业小白希望有人可以指点12,谢谢
import java.io.File;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class StegTool {
File input;
Bitmap img;
int wigth;
int height;
int x;
int y;
public StegTool(String inputName) throws IOException {
Bitmap img1 = BitmapFactory.decodeFile(inputName);
img = img1.copy(Bitmap.Config.ARGB_8888, true);
//得到图片的大小
wigth = img.getWidth();
height = img.getHeight();
x = 0;
y = 0;
}
//写入信息
public int write(String words) throws IOException{
int bit = 0;
int temp;
char[] c = words.toCharArray();
for(int i = 0; i < words.length(); i++){
for(int j = 0; j < 16; j++){//写入一个字符
temp= readIm();//从buf里面读取一个像素
bit = (c[i] >> (15-j)) & 0x0001;
temp = bit + (temp & 0xfffffffe);//修改像素
writeIm(temp);//将修改后的数据写入
if((x == img.getWidth()-1) && (y == img.getHeight()-1)){
return 0;
}else{
move();
}
}
}
return 1;
}
//读取信息
public String read(){
String words = new String("");
int temp;
char c = ' ';
do{
words += c;
c = 0;
for(int i = 0; i < 16; i++){
c *= 2;
temp = readIm() % 2;
if(temp != 0){//temp可能是-1
temp = 1;
}
c += temp;
if((x == img.getWidth()-1) && (y == img.getHeight()-1)){
return "该图片没有隐写信息";
}else{
move();
}
}
}while (c != '$');
return words;
}
private void move(){//移动到下一个位置
if((x == img.getWidth()-1) && (y < img.getHeight()-1)){
x = 0;
y++;
}else{
x++;
}
}
public void writeIm(int temp){
img.setPixel(x, y, temp);
}
private int readIm(){
return img.getPixel(x, y);
}
}
这段代码可以把一段输入的string存储到图片中,但是只能将TXT文档的路径存入图片,一个JAVA基础薄弱的非计算机专业小白希望有人可以指点12,谢谢