java和JS获取图片的宽和高

java和JS获取图片的宽和高

后台方法

jpg 和 png 格式

//    java获取图片的到达小,高度和宽度,jpg/png/
    public void imageSize() throws IOException {
//        方法一
        ImageIcon imageIcon = new ImageIcon("E:/ISiteImg/robot1.png");
        int width1 = imageIcon.getIconWidth();
        int height1 = imageIcon.getIconHeight();
        System.out.println("width1 = " + width1 + " - height1 =" + height1);   //        width1 = 519 - height1 =447
 
//        方法二
        File pic = new File("E:/ISiteImg/11.jpg");
        BufferedImage sourceImg = ImageIO.read(pic);
        int width = sourceImg.getWidth();
        int height = sourceImg.getHeight();
        System.out.println("width = " + width + " ; height = " + height);     //        width = 500 ; height = 304
    }

svg 格式

//    java 获取svg后缀的图片宽和高
    public void svgSize() throws IOException {
        File file = new File("E:/ISiteImg/2F PT_SIMPLE.svg");
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
        Document document = factory.createDocument(file.toURI().toString());   // import org.w3c.dom.Document;
        Element element = document.getDocumentElement();                       // import org.w3c.dom.Element;
        String viewBox = element.getAttribute("viewBox");
        System.out.println(" - viewBox = " + viewBox);    // - viewBox = 0 0 595.2 842
        String width = viewBox.split(" ")[2];  // splic(" ") 将字符串viewBox以空格分割为数组,下标为2代表宽,下标为3代表高
        String height = viewBox.split(" ")[3];
        int w = (int) Double.parseDouble(width);
        int h = (int) Double.parseDouble(height);
 
        System.out.println(" - -宽- " + w + "; - - 高-" + h);   //   - -宽- 595; - - 高-842
    }

前台获取方法 jpg 和 png 格式

在表单里面写好的上传组件,在回调函数 handleImageSuccess() 中写入读取宽和高的方法

                 <el-form-item label="上传底图" prop="image">
                        <el-upload
                                ref="uploadRef"
                                class="avatar-uploader"
                                style="margin-left: 10px"
                                action="/api/imageUpload"
                                :auto-upload="true"
                                :show-file-list="false"
                                :on-success="handleImageSuccess"
                        >
                            <img v-if="form.data.image" :src="'/api'+ form.data.image" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
function handleImageSuccess(res) {
        if (res.code === 1200) {
            let img_url = '/api' + res.data.image;
            console.log("- -- 1- - ", img_url);
            let img = new Image();
            img.src = img_url;
            img.onload = function(){
                console.log(" -img1- ", img.width, img.height);
            };
 
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值