Java 位运算基础

位运算是比较接近机器语言的运算方式,虽然比较消耗脑力,但很高效,必要时加上几句位运算的代码,会使程序跑的更快,同时,它也是一些专门场景中非常重要的一部分。现在就让我一起来了解一下吧!


1.位运算的表达式

运算符 含义 运算符 含义
按位取反 << 有符号数左移
& 按位与 >> 有符号数右移
| 按位或 >>> 无符号数右移
^ 按位异或

2.运算符的优先级和结合性

优先级 运算 结合性
1 ()、[]、.、new 自左向右
2 ~、!、++、–、(类型名)、+(正号)、-(负号) 自右向左
3 *、/、% 自左向右
4 +、- 自左向右
5 <<、>>、>>> 自左向右
6 >、>=、<、<=、instanceof 自左向右
7 ==、!= 自左向右
8 & 自左向右
9 ^ 自左向右
10 | 自左向右
11 && 自左向右
12 || 自左向右
13 ?: 自右向左
14 =、+=、-=、*=、/=、%=、&=、^=、|=、<<=、>>=、>>>= 自右向左

3.练习实例

针对位操作,这里提供一个对BMP格式图片的隐写练习。
原理:BMP图片是无损的,可针对色位最低位注入自己的数据,而图片显示不出明显的改动痕迹[实际是有的]。

代码详情:https://gitee.com/kxwinxp/secretbmp
注意:代码是针对24位BMP图片进行处理,兼容32位,但不支持低于24位

主体代码:


    /**
     * 方法体入口
     * InjectBmp(fileBmp,fileSecret)--->注入隐藏数据
     * SuckBmp(fileBmpou,fileRecovery)--->读出隐藏数据
     * @param args
     */
    public static void main(String[] args) {
        File fileBmp = new File("src/main/resources/origin.bmp");
        File fileSecret = new File("src/main/resources/origin.txt");
        //编译后路径
        //URL resource = Class.class.getClass().getResource("/origin.bmp");

        File fileBmpou=new File("src/main/resources/secret.bmp");
        File fileRecovery=new File("src/main/resources/secret.txt");

        try {
            //注入方法调用
            new testMain().InjectBmp(fileBmp,fileSecret);

            //提取方法调用
            byte[] result = new testMain().SuckBmp(fileBmpou,fileRecovery);
            System.out.println(new String(result, "UTF-8"));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 将文本信息从BMP图片取出
     *
     * @param fileBmp 存有隐藏数据的BMP图片路径
     * @param fileSecret 取出数据到 [可空]
     * @return 取出数据Bytes
     * @throws Exception
     */
    public byte[] SuckBmp(File fileBmp, File fileSecret) throws Exception {
        FileOutputStream ou = null;

        if (fileSecret != null) {
            if (fileSecret.exists()) {
                fileSecret.delete();
            }
            ou = new FileOutputStream(fileSecret);
        }
        BufferedImage bi = ImageIO.read(fileBmp);
        int width = bi.getWidth();
        int height = bi.getHeight();
        System.out.println("bmp:" + width + "*" + height + "=" + (width * height));
        //读取文件大小信息
        byte[] tranRead = BmpUtil.read(bi, new Position(), 8);
        long total = ByteUtil.getLong(tranRead, 0);

        //除去文件大小信息(8字节)的起点
        Position position = new Position((22) % bi.getWidth(), 22 / bi.getWidth(), 1);

        int len = 1024;
        System.out.println(total);
        byte[] allbt = null;
        while ((total - 1) / len > 1 || (len = (int) total) > 0) {
            total -= len;
            byte[] tranRead2 = BmpUtil.read(bi, position, len);
            allbt = (allbt == null) ? tranRead2 : ByteUtil.addBytes(allbt, tranRead2);
            if (ou != null) {
                ou.write(tranRead2);
                ou.flush();
            }
        }
        if (ou != null) {
            ou.close();
        }
        return allbt;
    }



    /**
     * 将文本信息注入BMP图片
     * 原理:图片一个像素[3原色(RGB),即3字节]可分离出3位,8像素可分离3字节
     *
     * @param fileBmp 要存隐藏数据的BMP图片路径
     * @param fileSecret 要存的隐藏数据路径
     * @throws Exception
     */
    public void InjectBmp(File fileBmp, File fileSecret) throws Exception {
        FileInputStream in = new FileInputStream(fileSecret);
        BufferedImage bi = ImageIO.read(fileBmp);

        int inSize = 1024;

        byte[] inbt = new byte[inSize];

        //8个字节所占像素约为22
        int pixel=8*8/3+(8*8%3==0?0:1);

        Position position = new Position(pixel % bi.getWidth(), pixel / bi.getWidth(), 1);

        long len = 0;
        while ((inSize = in.read(inbt)) != -1) {
            len += inSize;
            //每次注入inSize=1024个字节,并返回下一个像素位置[具体到3原色]
            position = BmpUtil.inject(bi, position, inbt, inSize);
        }
        // 将文本大小信息保存在图片头部(8字节[充当long类型]约占22个像素)
        byte[] end = new byte[8];
        ByteUtil.putLong(end, len, 0);

        System.out.println("total:" + len + " px" + position.x + ",py" + position.y);

        BmpUtil.inject(bi, new Position(), end, 8);
        String format = "bmp";
        File fileout = new File(fileBmp.getAbsolutePath().substring(0, fileBmp.getAbsolutePath().lastIndexOf(File.separator)), "secret." + format);
        ImageIO.write(bi, format, fileout);

    }

猜你喜欢

转载自blog.csdn.net/kxwinxp/article/details/79617502