权大师的网站是哪个公司做的,制作个人博客网站,域名跳转网站,邯郸网站设计怎么注册前言 本文是使用rust库resvg来将svg图片转为png图片。
环境配置 系统#xff1a;windows 平台#xff1a;visual studio code 语言#xff1a;rust 库#xff1a;resvg
代码分析
resvg是一个基于rust的svg渲染库#xff0c;其官方地址#xff1a; An SVG rendering li…前言 本文是使用rust库resvg来将svg图片转为png图片。
环境配置 系统windows 平台visual studio code 语言rust 库resvg
代码分析
resvg是一个基于rust的svg渲染库其官方地址 An SVG rendering library
resvg库的核心是svg的渲染但本文暂且不关注如何渲染svg本文关注如何将svg转为png格式官方有提供演示代码。
本文参考官方示例将代码稍作修改并结合rust的文件库rfd编写一个简单的程序可以导入svg图片然后转为png图片并保存。
首先看一下核心的转换代码 官方代码
fn main() {let args: VecString std::env::args().collect();if args.len() ! 3 {println!(Usage:\n\tminimal in-svg out-png);return;}let tree {let mut opt usvg::Options::default();// Get files absolute directory.opt.resources_dir std::fs::canonicalize(args[1]).ok().and_then(|p| p.parent().map(|p| p.to_path_buf()));opt.fontdb_mut().load_system_fonts();let svg_data std::fs::read(args[1]).unwrap();usvg::Tree::from_data(svg_data, opt).unwrap()};let pixmap_size tree.size().to_int_size();let mut pixmap tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).unwrap();resvg::render(tree, tiny_skia::Transform::default(), mut pixmap.as_mut());pixmap.save_png(args[2]).unwrap();
}本地使用时简单封装成一个函数如下
///
/// svg转png
///
pub fn svgtopng(svgpath: str,destimgpath: str,
)
{let mut optresvg::usvg::Options::default();opt.resources_dirstd::fs::canonicalize(svgpath).ok().and_then(|p| p.parent().map(|p| p.to_path_buf()));opt.fontdb_mut().load_system_fonts();let svgdatastd::fs::read(svgpath).unwrap();let treeresvg::usvg::Tree::from_data(svgdata,opt).unwrap();let pixmap_size tree.size().to_int_size();let mut pixmap resvg::tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).unwrap();resvg::render(tree, resvg::tiny_skia::Transform::default(), mut pixmap.as_mut());pixmap.save_png(destimgpath).unwrap();
}转换的程序就好了然后我们结合rust的GUI库iced来编写一个简单的带UI的转换程序所以我们还需要添加iced库看一下toml文件
[package]
name gui-serial
version 0.1.0
edition 2021[dependencies]iced{version0.12.1}
iced_widget{version0.12.3,features[]}
serialport4.3.0
clap4.5.7image0.25.1resvg{version0.42.0,features[]}关于iced以及rfd库的使用此处不再赘述可以参考本人的另外的博文 Rust UI开发三iced如何打开图片对话框并在窗口显示图片
我直接把主程序的代码贴在这
use std::{io::{self,Write}, process::CommandArgs};use eximg::codecs::png;
use imgtoicon::image_to_icon;
use resvg::usvg::filter::Merge;
use serialport::{DataBits,StopBits,Parity};
//use clap::{value_parser, Arg, ArgAction, Command};
mod ser;
mod imgtoicon;
mod resvgpro;
use iced::{Application, Command, Element, Font, Renderer, Settings, Subscription};
use iced_widget::{container,button,text,column,row,svg,image};use rfd::FileDialog;extern crate resvg;
extern crate image as eximg;#[derive(Debug,Clone)]
enum Message{Cvt,Open,Save,
}
struct Serial{portname:String,baudrate:u32,databits:DataBits,stopbits:StopBits,parity:Parity,timeout:u64,openfile:String,destfile:String,
}
fn main() -iced::Result {let myiconimgtoicon::image_to_icon(..\\gui-serial\\img\\mainicon4.png);let myfont微软雅黑;Serial::run(Settings{id:Some(mw.to_string()),window:iced::window::Settings{size:iced::Size{width:800.0,height:600.0},min_size:Some(iced::Size { width: 200.0, height: 200.0 }),max_size:Some(iced::Size { width: 1000.0, height: 800.0 }),position:iced::window::Position::Specific(iced::Point::new(100.0,100.0)),icon:Some(myicon),level:iced::window::Level::Normal,..Default::default()},default_font:Font::with_name(myfont),..Default::default()})}impl Application for Serial{type Executor iced::executor::Default;type Message Message;type Flags ();type Theme iced::Theme;fn new(flags: Self::Flags) - (Self, CommandSelf::Message) {(Self{portname:String::from(COM1),baudrate:9600,databits:DataBits::Eight,stopbits:StopBits::One,parity:Parity::None,timeout:1000,openfile:String::from(),destfile:String::from(),},Command::none())}fn title(self) - String {String::from(串口调试工具-rs)}fn update(mut self, message: Self::Message) - CommandSelf::Message {match message{Message::Cvt{resvgpro::svgtopng(self.openfile,self.destfile)}Message::Open{if let Some(file)FileDialog::new().set_title(打开文件).add_filter(svg, [svg]).pick_file(){self.openfilefile.display().to_string();}else{println!(打开文件失败)};}Message::Save{if let Some(file)FileDialog::new().set_title(保存文件).add_filter(png, [png]).save_file(){let filestrfile.display().to_string();resvgpro::svgtopng(self.openfile, filestr);self.destfilefilestr;}else{println!(保存文件失败)};}}Command::none()}fn subscription(self) - SubscriptionSelf::Message {Subscription::none()}fn view(self) - Element_, Self::Message, Self::Theme, crate::Renderer {//let btn1button(转换).on_press(Message::Cvt);let btn2button(打开).on_press(Message::Open);let btn3button(转换并保存).on_press(Message::Save);let svghandlesvg::Handle::from_path(self.openfile);let pnghandleimage::Handle::from_path(self.destfile);let col1column![btn2,text(format!(打开文件路径:{},self.openfile)).size(15),btn3,text(format!(保存文件路径:{},self.destfile)).size(15),//btn1,row![svg(svghandle).content_fit(iced::ContentFit::Contain).width(300),image(pnghandle).content_fit(iced::ContentFit::Contain).width(300),].padding(5).spacing(20),].padding(5).spacing(5);let contcontainer(col1).padding(5);cont.into()}
}
实例演示 文章转载自: http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.ymwnc.cn.gov.cn.ymwnc.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.hilmwmu.cn.gov.cn.hilmwmu.cn http://www.morning.bbyqz.cn.gov.cn.bbyqz.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.jfxth.cn.gov.cn.jfxth.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.bqdgr.cn.gov.cn.bqdgr.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.mymz.cn.gov.cn.mymz.cn