如何加载图像和隐藏文本(styganography)

时间:2016-04-30 16:04:47

标签: java android bitmap

在我的算法中,我成功隐藏了文本,但仅在我加载小png图像时 当我加载大图像(例如547 ko)时,应用程序停止了 这是我的代码:

这是onCreat

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView=(ImageView)findViewById(R.id.imageView);
}

我在这个场景中加载图像

public void openImage(View view){
    Toast.makeText(this, "select photo", Toast.LENGTH_SHORT).show();

    Intent intent=new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    startActivityForResult(intent, 1);
}

这是用于拍摄图像

public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    super.onActivityResult(requestCode, resultCode, resultData);

    if (requestCode == 1 && resultCode == RESULT_OK) {
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();
            try {
                bitmap = getBitmapFromUri(uri);
                imageView.setImageBitmap(bitmap);
                steganographie();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else
        Toast.makeText(this,"ERROR LOAD IMAGE",Toast.LENGTH_SHORT).show();
}

并且代码如何隐藏文本

    private void steganographie() {

    ////Declaration /////////////////////////////
    String text ="text to hide";
    boolean bool=false;
    String binarytext = new BigInteger(text.getBytes()).toString(2);
    char [] textCharTab=binarytext.toCharArray();
    int nbrbit=  textCharTab.length;
    //////////////////////////////////

    ///Algorithem Add hide text
    BitmapStegan =Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    for(int i=0; i<bitmap.getWidth(); i++){
        for(int j=0; j<bitmap.getHeight(); j++){
            int pixel=bitmap.getPixel(i, j);
            int rouge= Color.red(pixel);
            int vert=Color.green(pixel);
            int bleu=Color.blue(pixel);
            // int alpha=Color.alpha(pixel);
            if (i==20&&j==2){
                bool=true;
            }else{}
            if(nbrbit>0 && bool == true){
                String b=Integer.toBinaryString(rouge);
                char [] tabInt=b.toCharArray();
                if(nbrbit!=1){
                    tabInt[7]= textCharTab[nbrbit-1];
                    tabInt[6]= textCharTab[nbrbit-2];
                }else{
                    tabInt[7]= textCharTab[nbrbit-1];
                }
                nbrbit=nbrbit-2;
                String bitchan=String.copyValueOf(tabInt);
                rouge=stringBitToInt(bitchan);
            }
            //BitmapStegan.setPixel(i, j, Color.argb(Color.alpha(pixel), rouge, vert, bleu));
        }
    }
    imageView.setImageBitmap(BitmapStegan);
    Toast.makeText(this,"I am here",Toast.LENGTH_LONG).show();

}

private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return bitmap;
}

private int stringBitToInt(String bitchan) {
    char[] tab=bitchan.toCharArray();
    double n=0;
    int p=tab.length;
    for (int i = 0; i <tab.length; i++) {
        p--;
        if(tab[i]=='1'){
            n=n+Math.pow(2, p);
        }else{}
    }
    int result= (int)n;
    return result;
}

}

0 个答案:

没有答案