</pre>没弄懂原理写个鬼代码啊(摔<p></p><p>还是觉得MATLAB超级不好用....要不是库多hhhhh</p><p>还是去看原理去了(逃</p><p></p><p>这是加密代码</p><p></p><pre name="code" class="java">package wjoker;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class jiami {
static String host = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\6_1.jpg";
static String key = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\key.jpg";
static String output = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\out1.jpg";
//static String output2 = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\out2.jpg";
static String readImageFormat = "jpg";
static String writeImageFormat = "jpg";
static int a_x = 0;
static int a_y = 0;
static int b_x = 7;
static int b_y = 0;
static int weight = 400;
static int hight = 560;
static int block = 8;
static int weight_size = weight/block;
static int hight_size = hight/block;
public static void main(String[] args) {
BufferedImage bi_host = null;
BufferedImage bi_key = null;
BufferedImage bi_output = null;
bi_host = input_img(host, readImageFormat);
bi_key = input_img_min(key, readImageFormat);
bi_output = bi_host;
System.out.println((bi_host == null));
System.out.println((bi_output == null));
change(bi_host, bi_key, bi_output);
output_img(bi_output, writeImageFormat, output);
}
public static BufferedImage input_img(String input_File,String readImageFormat){
FileInputStream fis = null;
ImageInputStream iss = null;
BufferedImage bi = null;
try {
fis = new FileInputStream(input_File);
Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);
ImageReader reader = (ImageReader) it.next();
try {
iss = ImageIO.createImageInputStream(fis);
reader.setInput(iss,true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(0,0,400,560);
param.setSourceRegion(rect);
bi = reader.read(0,param);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bi;
}
public static BufferedImage input_img_min(String input_File,String readImageFormat){
FileInputStream fis = null;
ImageInputStream iss = null;
BufferedImage bi = null;
try {
fis = new FileInputStream(input_File);
Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);
ImageReader reader = (ImageReader) it.next();
try {
iss = ImageIO.createImageInputStream(fis);
reader.setInput(iss,true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(0,0,100,140);
param.setSourceRegion(rect);
bi = reader.read(0,param);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bi;
}
public static void output_img(BufferedImage bi,String writeImageFormat,String output_File){
try {
ImageIO.write(bi, writeImageFormat, new File(output_File));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static BufferedImage change(BufferedImage bi_host,BufferedImage bi_key,BufferedImage bi_output){
int i,j;
int rgb_a,rgb_b,rgb_key;
boolean flag;
for(i=0;i<weight_size;i++){
for(j=0;j<hight_size;j++){
rgb_a = getGrey(bi_host.getRGB(i*block+a_x, j*block+a_y));
rgb_b = getGrey(bi_host.getRGB(i*block+b_x, j*block+b_y));
rgb_key = getGrey(bi_key.getRGB(i, j));
if(rgb_a>rgb_b)//key = 0
flag = true;
else //key = 256
flag = false;
if(rgb_key > 0){
if(flag){
int tmp = (rgb_a+rgb_b)/2;
rgb_a = tmp-1;
rgb_b = tmp+1;
}
} else {
if(!flag){
int tmp = (rgb_a+rgb_b)/2;
rgb_a = tmp+1;
rgb_b = tmp-1;
}
}
bi_output.setRGB(i*block+a_x, j*block+a_y, new Color(rgb_a,rgb_a,rgb_a).getRGB());
bi_output.setRGB(i*block+b_x, j*block+b_y, new Color(rgb_b,rgb_b,rgb_b).getRGB());
}
}
return bi_output;
}
public static int getGrey(int rgb){
int r = (rgb >> 16) & 0xff;//取出次高位(16-23)红色分量的信息
int g = (rgb >> 8) & 0xff;//取出中位(8-15)绿色分量的信息
int b = rgb & 0xff;//取出低位(0-7)蓝色分量的信息
return (r+g+b)/3;
}
}
这是解密代码
package wjoker;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class jiemi {
static String host = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\out1.jpg";
static String key = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\key.jpg";
//static String output = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\out1.jpg";
static String output = "H:\\Workspaces\\MyEclipse 2015 CI\\DCT\\src\\out2.jpg";
static String readImageFormat = "jpg";
static String writeImageFormat = "jpg";
static int a_x = 0;
static int a_y = 0;
static int b_x = 7;
static int b_y = 0;
static int weight = 400;
static int hight = 560;
static int block = 8;
static int weight_size = weight/block;
static int hight_size = hight/block;
public static void main(String[] args) {
BufferedImage bi_host = null;
BufferedImage bi_key = null;
BufferedImage bi_output = null;
bi_host = input_img(host, readImageFormat);
bi_key = input_img_min(key, readImageFormat);
bi_output = bi_key;
output_img(bi_output, writeImageFormat, output);
}
public static BufferedImage input_img(String input_File,String readImageFormat){
FileInputStream fis = null;
ImageInputStream iss = null;
BufferedImage bi = null;
try {
fis = new FileInputStream(input_File);
Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);
ImageReader reader = (ImageReader) it.next();
try {
iss = ImageIO.createImageInputStream(fis);
reader.setInput(iss,true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(0,0,400,560);
param.setSourceRegion(rect);
bi = reader.read(0,param);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bi;
}
public static BufferedImage input_img_min(String input_File,String readImageFormat){
FileInputStream fis = null;
ImageInputStream iss = null;
BufferedImage bi = null;
try {
fis = new FileInputStream(input_File);
Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);
ImageReader reader = (ImageReader) it.next();
try {
iss = ImageIO.createImageInputStream(fis);
reader.setInput(iss,true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(0,0,100,140);
param.setSourceRegion(rect);
bi = reader.read(0,param);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bi;
}
public static void output_img(BufferedImage bi,String writeImageFormat,String output_File){
try {
ImageIO.write(bi, writeImageFormat, new File(output_File));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int getGrey(int rgb){
int r = (rgb >> 16) & 0xff;//取出次高位(16-23)红色分量的信息
int g = (rgb >> 8) & 0xff;//取出中位(8-15)绿色分量的信息
int b = rgb & 0xff;//取出低位(0-7)蓝色分量的信息
return (r+g+b)/3;
}
public static void change(BufferedImage bi_host,BufferedImage bi_output){
int i,j;
int rgb_a,rgb_b;
boolean flag;
for(i=0;i<weight_size;i++){
for(j=0;j<hight_size;j++){
rgb_a = getGrey(bi_host.getRGB(i*block+a_x, j*block+a_y));
rgb_b = getGrey(bi_host.getRGB(i*block+b_x, j*block+b_y));
if(rgb_a>rgb_b)
bi_output.setRGB(i, j, new Color(0,0,0).getRGB());
else
bi_output.setRGB(i, j, new Color(255,255,255).getRGB());
}
}
}
}
还是那个坑爹的绝对路径烦死爹了,
好了,荆轲刺秦王.