java图片转二进制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35517448/article/details/82584884
本地图片转二进制数组
  /**
     * 图片转二进制数组
     * @param imgType 
     * @param url
     * @return
     */
    public  static byte[] getImageBinary(String path, String imgType) {
            File f = new File(path);
            BufferedImage bi;
            try {
                bi = ImageIO.read(f);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(bi, imgType, baos);  //经测试转换的图片是格式这里就什么格式,否则会失真
                byte[] bytes = baos.toByteArray();

                return bytes;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;

    }
二进制数组转字符串
    /**
     * 二进制数组转字符串
     * @param path
     * @return 
     */
    public static String byteToString(byte[] b) {
          StringBuffer sb = new StringBuffer();  
          String stmp = "";  
          for (int i = 0; i < b.length; i++) {  
           stmp = Integer.toHexString(b[i] & 0XFF);  
           if (stmp.length() == 1){  
               sb.append("0" + stmp);  
           }else{  
               sb.append(stmp);  
           }  
          }
          String sss = sb.toString();
          return sss;
    }
通过图片的链接获取二进制数组
    /**
     * 通过图片链接获取二进制数组
     * @param URLName
     * @param type
     * @return 二进制的字符串
     */
    public static String getImgeHexString(String URLName,String type) {   
        String res = null;   
        try {   
            int HttpResult = 0; // 服务器返回的状态   
            URL url = new URL(URLName); // 创建URL   
            URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码   
            ((HttpURLConnection) urlconn).setRequestMethod("POST");
            urlconn.setConnectTimeout(6 * 1000);
            urlconn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");

            urlconn.connect();   
            HttpURLConnection httpconn = (HttpURLConnection) urlconn;   
            HttpResult = httpconn.getResponseCode();   
//          System.out.println(HttpResult);   
            if (HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK则连接不成功   
                System.out.print("fail");   
            else {   
                BufferedInputStream bis = new BufferedInputStream(urlconn.getInputStream());   

                BufferedImage bm = ImageIO.read(bis);   
                ByteArrayOutputStream bos = new ByteArrayOutputStream();   
                ImageIO.write(bm, type, bos);   
                bos.flush();   
                byte[] data = bos.toByteArray();   

                res = byte2hex(data);   
                bos.close();   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
        return res;   
    }   
    /**  
     * 格式化byte  
     *   
     * @param b  
     * @return  
     */  
    public static String byte2hex(byte[] b) {   
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',   
                'B', 'C', 'D', 'E', 'F' };   
        char[] out = new char[b.length * 2];   
        for (int i = 0; i < b.length; i++) {   
            byte c = b[i];   
            out[i * 2] = Digit[(c >>> 4) & 0X0F];   
            out[i * 2 + 1] = Digit[c & 0X0F];   
        }   

        return new String(out);   
    }

猜你喜欢

转载自blog.csdn.net/qq_35517448/article/details/82584884