一、前言 在工作中,難免遇到各種各樣的問題,每個人似乎都有一套自己的解決方案。而我,又不想每次解決完問題就把東西扔了,撿了芝麻,丟了西瓜,什麼時候才能進步勒?學習要靠積累,畢竟量變才能引起質變嘛。所以寫了這篇博文,不定時更新自己項目中遇到的問題、踩過的那些坑...... 二、項目 1、Java 將兩 ...
一、前言
在工作中,難免遇到各種各樣的問題,每個人似乎都有一套自己的解決方案。而我,又不想每次解決完問題就把東西扔了,撿了芝麻,丟了西瓜,什麼時候才能進步勒?學習要靠積累,畢竟量變才能引起質變嘛。所以寫了這篇博文,不定時更新自己項目中遇到的問題、踩過的那些坑......
二、項目
1、Java 將兩張圖片合成一張圖片
/** * 圖片合併 */ public String joinImage(String url1, String url2) { try { InputStream is1 = getImgConn(url1); InputStream is2 = getImgConn(url2); BufferedImage image1 = ImageIO.read(is1); BufferedImage image2 = ImageIO.read(is2); BufferedImage combined = new BufferedImage(image1.getWidth() * 2, image1.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); g.drawImage(image1, 0, 0, null); g.drawImage(image2, image1.getWidth(), 0, null); String imgURL=System.currentTimeMillis()+".jpg"; ImageIO.write(combined, "JPG", new File("/home/wwwroot/picFiles",imgURL)); return "/home/wwwroot/picFiles/"+imgURL; } catch (IOException e) { e.printStackTrace(); } return null; } //讀文件 private InputStream getImgConn(String url){ try { URL url1 = new URL(url); URLConnection urlConnection = url1.openConnection(); InputStream is1 = urlConnection.getInputStream(); //先讀入記憶體 ByteArrayOutputStream buf = new ByteArrayOutputStream(8192); byte[] b = new byte[1024]; int len; while ((len = is1.read(b)) != -1) { buf.write(b, 0, len); } is1=new ByteArrayInputStream(buf.toByteArray()); return is1; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }圖片是 http 協議的伺服器上的文件
/** * 圖片合併 */ public static String joinImage(File file1, File file2) { BufferedImage image1 = null; BufferedImage image2 = null; try { image1 = ImageIO.read(file1); image2 = ImageIO.read(file2); BufferedImage combined = new BufferedImage(image1.getWidth() * 2, image1.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); g.drawImage(image1, 0, 0, null); g.drawImage(image2, image1.getWidth(), 0, null); String imgURL=System.currentTimeMillis()+".jpg"; ImageIO.write(combined, "JPG", new File("/home/wwwroot/picFiles",imgURL)); return "/home/wwwroot/picFiles/"+imgURL; } catch (IOException e) { e.printStackTrace(); } return null; }圖片是本地文件
2、通過經緯度獲得高德地圖返回的具體地址
/** * 獲取經緯度的確切位置 * @param lat 經度 * @param lon 維度 * @param type type 001 (100代表道路,010代表POI,001代表門址,111可以同時顯示前三項) * @return */ private String getLocByLonAndLat(String lat,String lon,String type){ String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+lon+"&type="+type; String res = ""; BufferedReader in=null; try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { res += line+"\n"; } JSONObject jsonObject = JSONObject.fromObject(res); JSONArray addrList =(JSONArray)jsonObject.get("addrList"); JSONObject o =(JSONObject)addrList.get(0); String s = o.get("admName").toString() + o.get("addr").toString(); return StringUtils.remove(s,","); } catch (Exception e) { System.out.println("error in wapaction,and e is " + e.getMessage()); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }View Code