【Java】------如何使用base64格式获取图片长、宽度。

一、如何使用base64格式获取图片长、宽度。支持图片类型有:svg、png、jpg、jpeg等。

1.java实例代码如下:(代码中存在封装后方法,复制过去会出现错误提示,属于正常现象、需要自己微改!) 

	//获取SVG长宽
	public static JSONObject fn_SVG_WIDTH_HEIGHT(String svgURI) throws Exception {
		File file = new File(svgURI);
		String parser = XMLResourceDescriptor.getXMLParserClassName();
		SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
		Document doc = f.createDocument(file.toURI().toString());
		Element element = doc.getDocumentElement();
		String width = element.getAttribute("width");
		String height = element.getAttribute("height");
		JSONObject svgJson=new JSONObject();
		svgJson.put("width",width);
		svgJson.put("height",height);

		return svgJson;
	}

	//获取ICON长宽
	public JSONObject fn_get_file_width_height(String v_BAES64,FileInfo info)throws Exception{

		JSONObject jsonObject=new JSONObject();
		jsonObject.put("width","");
		jsonObject.put("height","");
		// 图片对象
		String fileBase="";
		if(v_BAES64.equals("")==false){
			if(v_BAES64.contains("png")){
				fileBase=v_BAES64.replaceAll("data:image/png;base64,", "");
			}else if(v_BAES64.contains("jpg")){
				fileBase=v_BAES64.replaceAll("data:image/jpg;base64,", "");
			}else if(v_BAES64.contains("svg")){
				String len="data:image/svg+xml;base64,";
				fileBase=v_BAES64.substring(len.length(),v_BAES64.length());
			}else if(v_BAES64.contains("jpeg")){
				fileBase=v_BAES64.replaceAll("data:image/jpeg;base64,", "");
			}else if(v_BAES64.contains("gif")){
				fileBase=v_BAES64.replaceAll("data:image/gif;base64,", "");
			}
		}

		if(fileBase.equals("")==false){
			if(info.getType().equals("image/svg+xml")==false){
				byte [] decoder = new BASE64Decoder().decodeBuffer(fileBase);
				InputStream fileStream=new ByteArrayInputStream(decoder);
				BufferedImage bufferedImage = ImageIO.read(fileStream);
				if(bufferedImage!=null){
					// 宽度
					int width = bufferedImage.getWidth();
					// 高度
					int height = bufferedImage.getHeight();

					jsonObject.put("width",width);
					jsonObject.put("height",height);
					bufferedImage = null;
					fileStream.close();
				}
			}else{
				try {
					byte [] decoder = new BASE64Decoder().decodeBuffer(fileBase);
					for (int i = 0; i < decoder.length; ++i) {
						if (decoder[i] < 0) {// 调整异常数据
							decoder[i] += 256;
						}
					}
					File file = new File(com.imx.tool.AppConfigFileHelper.get_UploadFolder() + "\\" + tool.IDHelper.NewGUID());
					// 如果要返回file文件这边return就可以了,存到临时文件中
					OutputStream out = new FileOutputStream(file.getPath());
					out.write(decoder);
					out.flush();
					out.close();
					JSONObject svgJson=fn_SVG_WIDTH_HEIGHT(file.getPath());
					jsonObject.put("width",svgJson.getString("width"));
					jsonObject.put("height",svgJson.getString("height"));
					tool.FileHelper.DelFile(file.getPath());//删除本地文件
				} catch (Exception e) {

				}
			}
		}
		return  jsonObject;
	}

最后效果图:如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰要做大神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值