Google的图片处理库和条码处理库、汉字转拼音库

  • Thumbnailator
  • zxing
    • 生成二维码的Base64字符串
  • JPinyin 汉字转拼音的Java类库
    • 引入依赖
    • 使用示例
    • 结果输出

Thumbnailator

Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比Java API的好。从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量。还支持对一个目录的所有图片进行批量处理操作。

Thumbnailator Github

支持的处理操作:图片缩放,区域裁剪,水印,旋转,保持比例。
另外值得一提的是,Thumbnailator至今仍不断更新,怎么样,感觉很有保障吧!

引入库

    // https://mvnrepository.com/artifact/net.coobird/thumbnailatorcompile group: 'net.coobird', name: 'thumbnailator', version: '0.4.8'

原图

加水印

    @Testpublic void testThumbnailitor(){try {Thumbnails.of("F:\\images\\img1.jpg").size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("F:\\images\\watermark.jpg")), 0.5f).outputQuality(0.8f).toFile("F:\\images\\imgtemp.jpg");} catch (IOException e) {e.printStackTrace();}try {Thumbnails.of("F:\\images\\img1.jpg").size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File("F:\\images\\watermark.jpg")), 0.5f).outputQuality(0.8f).toFile("F:\\images\\imgtemp2.png");} catch (IOException e) {e.printStackTrace();}}


保持比例缩小图片

            Thumbnails.of("F:\\images\\img1.jpg").size(120, 100).toFile("F:\\images\\imgtemp3.jpg");


旋转图片

            Thumbnails.of("F:\\images\\img1.jpg").size(800, 800).rotate(90).toFile("F:\\images\\imgtemp4.jpg");Thumbnails.of("F:\\images\\img1.jpg").size(800, 800).rotate(-45).toFile("F:\\images\\imgtemp5.jpg");


降低图片质量压缩图片

            Thumbnails.of("F:\\images\\img1.jpg").scale(1).outputQuality(0.1).toFile("F:\\images\\imgtemp6.jpg");

zxing

ZXing是一个Google开发开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。该项目可实现的条形码编码和解码。目前支持以下格式:QR码、UPC-A、UPC-E、EAN-8,EAN-13、39码、93码、代码128、RSS-14(所有的变体)、RSS扩展(大多数变体)、数据矩阵;

zxing Github

加入引用

    // https://mvnrepository.com/artifact/com.google.zxing/corecompile group: 'com.google.zxing', name: 'core', version: '3.4.0'
// https://mvnrepository.com/artifact/com.google.zxing/javasecompile group: 'com.google.zxing', name: 'javase', version: '3.4.0'

生成一个二维码到文件

String filePath = "D://";String fileName = "zxing.png";JSONObject json = new JSONObject();json.put("title" ,   "https://www.baidu.com");json.put("author", "shihy");String content = json.toJSONString();// 内容int width = 200; // 图像宽度int height = 200; // 图像高度String format = "png";// 图像类型Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");BitMatrix bitMatrix = null;// 生成矩阵try {bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height, hints);Path path = FileSystems.getDefault().getPath(filePath, fileName);MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像System.out.println("输出成功.");} catch (WriterException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}



从文件解析二维码内容

      String filePath = "D://zxing.png";BufferedImage image;try {image = ImageIO.read(new File(filePath));LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码JSONObject content = JSONObject.parseObject(result.getText());System.out.println("图片中内容:  ");System.out.println("author: " + content.getString("author"));System.out.println("zxing:  " + content.getString("title"));System.out.println("图片中格式:  ");System.out.println("encode: " + result.getBarcodeFormat());} catch (IOException e) {e.printStackTrace();} catch (NotFoundException e) {e.printStackTrace();}


生成二维码到OutputStream里,在网页显示

public class QuickResponse {public  static BitMatrix generateQRCodeByJson(JSONObject json , int imgWidth , int imgHeight ,String imgformat) {Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");BitMatrix bitMatrix = null;// 生成矩阵try {bitMatrix = new MultiFormatWriter().encode(json.toJSONString(),BarcodeFormat.QR_CODE, imgWidth, imgHeight, hints);return bitMatrix;} catch (WriterException e) {e.printStackTrace();}return null;}public  static BitMatrix generateQRCodeByString(String content , int imgWidth , int imgHeight ,String imgformat){Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");BitMatrix bitMatrix = null;// 生成矩阵try {bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, imgWidth, imgHeight, hints);return bitMatrix;} catch (WriterException e) {e.printStackTrace();}return null;}public static String decodeQRCode(String filePath){BufferedImage image;try {image = ImageIO.read(new File(filePath));LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码return result.getText();} catch (IOException e) {e.printStackTrace();} catch (NotFoundException e) {e.printStackTrace();}return null;}}
    @RequestMapping("/generate")public String generateQRcodePicture(HttpServletResponse response){BitMatrix bitMatrix = QuickResponse.generateQRCodeByString("http://www.baidu.com", 120, 120, "png");try {MatrixToImageWriter.writeToStream(bitMatrix ,"png" ,response.getOutputStream());} catch (IOException e) {e.printStackTrace();}return "index";}@RequestMapping("/other")public String useQRcode(){return "otherpage";}
在这里插入代码片
<body>
<h2>see you</h2>
<img src="/generate" width="100px" height="100px" >
</body>


生成二维码的Base64字符串

    /*** 生成二维码的Base64字符串* @param content* @param width* @param height* @return* @throws IOException*/public String crateQRCode(String content, int width, int height) throws IOException {String resultImage = "";if (!StringUtils.isEmpty(content)) {ServletOutputStream stream = null;ByteArrayOutputStream os = new ByteArrayOutputStream();@SuppressWarnings("rawtypes")HashMap<EncodeHintType, Comparable> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 指定字符编码为“utf-8”hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 指定二维码的纠错等级为中级hints.put(EncodeHintType.MARGIN, 2); // 设置图片的边距try {QRCodeWriter writer = new QRCodeWriter();BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);ImageIO.write(bufferedImage, "png", os);/*** 原生转码前面没有 data:image/png;base64 这些字段,返回给前端是无法被解析,可以让前端加,也可以在下面加上*/String base64str = org.springframework.util.Base64Utils.encodeToString(os.toByteArray());resultImage = new String("data:image/png;base64," + base64str);return resultImage;} catch (Exception e) {e.printStackTrace();} finally {if (stream != null) {stream.flush();stream.close();}}}return null;}

使用示例

log.debug(crateQRCode("https://www.baidu.com",360,360));

打印输出

10:37:06.588 [main] DEBUG cn.common.component.TestClass2 - data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWgAAAFoAQAAAABSnlx4AAABS0lEQVR4Xu3WUWrEMAyEYUMOlqv7YAtpNWMr3hjK7mPRP4RW0XzJk0nbrm/Snos/g96D3oPeg97zn/WrZc7f+bj6EctT112hy+uYPOSVt1mh0To9K+3L2RoVGr2ulKB7hd6rmlpDzrlBo0NfigZ/dNr89KwVurjORP1+3RW6uH7EwqfqUaEra227jBNofUZzLNGVtbsjfvd32o/5FrXo4tqrc0VysdF86eSh0XHj2Z0v3Y53oYtrHaYoms5QdvOBiAd0bT1WazG/RL6No3ahq+vM+boPlofYuEK36nogHx3NOfT5DBpt7SG2pjOjRRfXa1T0Nh/WBo32qcq46O7k4vKMLq9jmnXzedIr0nXP6OLanQaLNWj0rl3HMn+i0Q+tbmz850rVeAW6uE6UB2gu49bvQpfXmfOBDs3+/wZdXH8e9B70HvQe9J4a+gerJQhKBHZa5AAAAABJRU5ErkJggg==


JPinyin 汉字转拼音的Java类库

JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进。
【JPinyin主要特性】
1、准确、完善的字库;
Unicode编码从4E00-9FA5范围及3007(〇)的20903个汉字中,JPinyin能转换除46个异体字(异体字不存在标准拼音)之外的所有汉字;
2、拼音转换速度快;
经测试,转换Unicode编码从4E00-9FA5范围的20902个汉字,JPinyin耗时约100毫秒。
3、多拼音格式输出支持;
JPinyin支持多种拼音输出格式:带音标、不带音标、数字表示音标以及拼音首字母输出格式;
4、常见多音字识别;
JPinyin支持常见多音字的识别,其中包括词组、成语、地名等;
5、简繁体中文转换

引入依赖

Maven依赖:

        <dependency><groupId>com.github.stuxuhai</groupId><artifactId>jpinyin</artifactId><version>1.1.8</version></dependency>

Gradle依赖:

// https://mvnrepository.com/artifact/com.github.stuxuhai/jpinyin
compile group: 'com.github.stuxuhai', name: 'jpinyin', version: '1.1.8'

使用示例

    @Testpublic void test6() throws PinyinException {String str = "你好世界,我叫吃鱼王正。";/*** 将字符串转换成相应格式的拼音** Params:* str – 需要转换的字符串* separator – 拼音分隔符* pinyinFormat – 拼音格式:WITH_TONE_NUMBER--数字代表声调,WITHOUT_TONE--不带声调,WITH_TONE_MARK--带声调* Returns:* 字符串的拼音*/String str1 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_MARK); // nǐ,hǎo,shì,jièString str2 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_NUMBER); // ni3,hao3,shi4,jie4String str3 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE); // ni,hao,shi,jie/*** 获取字符串对应拼音的首字母** Params:* str – 需要转换的字符串* Returns:* 对应拼音的首字母*/String str4 = PinyinHelper.getShortPinyin(str); // nhsjSystem.out.println(str1);System.out.println(str2);System.out.println(str3);System.out.println(str4);/*** PinyinHelper.hasMultiPinyin('已')* 判断字符是否是多音字*/
//        还 是多音字if( PinyinHelper.hasMultiPinyin('还')){System.out.println(" 还 :是多音字");}
//        王 是多音字if( PinyinHelper.hasMultiPinyin('王')){System.out.println(" 王 :是多音字");}
//        周 不是多音字if( PinyinHelper.hasMultiPinyin('周')){System.out.println(" 周 :是多音字");}/*** 将单个汉字转换成带声调格式的拼音** Params:* c – 需要转换成拼音的汉字* Returns:* 字符串的拼音*/String[] strArray = PinyinHelper.convertToPinyinArray('王');for (int i = 0; i < strArray.length; i++) {System.out.printf(strArray[i]+" , ");}}

结果输出

nǐ,hǎo,shì,jiè,,,wǒ,jiào,chī,yú,wáng,zhèng,。
ni3,hao3,shi4,jie4,,,wo3,jiao4,chi1,yu2,wang2,zheng4,。
ni,hao,shi,jie,,,wo,jiao,chi,yu,wang,zheng,。
nhsj,wjcywz。还 :是多音字王 :是多音字
wáng , wàng ,

Google的图片处理库和条码处理库、汉字转拼音库相关推荐

  1. java 拼音_GitHub - promeG/TinyPinyin: 适用于Java和Android的快速、低内存占用的汉字转拼音库。...

    TinyPinyin 适用于Java和Android的快速.低内存占用的汉字转拼音库. 当前稳定版本:2.0.3 特性 生成的拼音不包含声调,均为大写: 支持自定义词典,支持简体中文.繁体中文: 执行 ...

  2. Python 汉字转拼音库 pypinyin, 附:汉字拼音转换工具

    一.初衷: 一些开源软件的配置文件中识别区分的部分用英文,那么我们在批量生成配置文件的时候,可以从CMDB导入汉字(idc_name), 然后将它转换成拼音,再或者拼接上IP地址,以便更准确的识别.例 ...

  3. 轻巧的汉字转拼音库 TinyPinyin 在Android上的使用

    最近发现一个相当轻巧,运行速度很快的汉字转拼音库--TinyPinyin,这个汉字转拼音库比上一篇讲述列表按照A-Z的规则排序的文章所使用的汉字转拼音库运行速度还要快10倍以上. 主要特性 生成的拼音 ...

  4. php汉字转拼音库,汉字转拼音的PHP库

    汉字转拼音的PHP库 namespace Overtrue\Pinyin; use InvalidArgumentException; define('PINYIN_NONE', 'none'); d ...

  5. Java汉字转拼音库,Pinyin4j

    pinyin4j是一个支持将简体和繁体中文转换到成拼音的Java开源类库,作者是Li Min (xmlerlimin@gmail.com).以下是一些具体的介绍和使用方式.         1.pin ...

  6. 汉字转拼音,TinyPinyin、Pinyin4j与JPinyin哪个库更快

    1. 介绍 本文对TinyPinyin.Pinyin4j与JPinyin三个汉字转拼音库的用法.测试代码及转换的结果做一个简单的总结. TinyPinyin 适用于Java和Android的快速.低内 ...

  7. python第三方库:pypinyin将汉字转为拼音

    汉字的拼音虽然有一定的规律,但是做一套好的汉字转拼音的系统并不是那么容易,需要考虑的问题也比较多.汉字转拼音在多个的方向上也经常使用到.比如在url中,很少使用中文作为url连接,一种方式是转换为拼音 ...

  8. 使用Google WebP图片格式帮助控制网站页面大小

    日期:2013-3-16  来源:GBin1.com 不管你相信或者不相信,随着互联网的快速发展网页也在持续不断的变大. 使 网页迅速膨胀的罪魁祸首不是大量使用的JavaScript库,CSS和无尽的 ...

  9. 以图搜图 - Google 相似图片搜索原理 - Java实现

    转自:http://blog.csdn.net/luohong722/article/details/7100058 前阵子在阮一峰的博客上看到了这篇<相似图片搜索原理>博客,就有一种冲动 ...

  10. WIN下免费pdf转图片PNG/JPG/TIFF软件,poppler和pdftocairo开源库编译,中文文件名出错修正

    wxleasyland@139.com 2021.12 想要一个打印到图片的虚拟打印机软件,结果都是收费的.自已编是不可能的,工作量太大. 现在PDF虚拟打印机很多,可以打成PDF,再转成图片. 想要 ...

最新文章

  1. JS window.open()属性
  2. 【数字智能三篇】之三: 一页纸说清楚“什么是深度学习?”
  3. 神策数据出席 TIC 2018 大会,共同赋能大数据时代
  4. Python多线程详解
  5. leetcode -39组合总数
  6. c语言iso校验算法,模式识别c语言ISODATA算法.doc
  7. c语言程序设计黄保和第二章,c语言程序设计答案(选择题+编程)黄保和、江戈版...
  8. 毕业设计答辩PPT模板
  9. 智方8000系进销存管理系统 v11.29 绿色
  10. 【老生谈算法】matlab实现K均值聚类算法——K均值聚类算法
  11. Kafka生产者、消费者的消息可靠性方案实现
  12. SpringBoot自动装配
  13. 浅析计算机用户身份识别技术,(浅析身份认证技术.doc
  14. 今日学习在线编程题:可怜的小码哥
  15. JavaScript函数isFinite()
  16. 如何制作实时库存报表
  17. IP地址常见分类:A类、B类、C类、D类、E类
  18. 如何避免干井校准操作的常见误区?有效执行温度校准
  19. ai修复图片 python_百度AI攻略:拉伸图像恢复
  20. Android 多种简单的弹出框样式设置

热门文章

  1. git stage 暂存_git学习小计(二):常用命令、index暂存区
  2. python实现熵权法
  3. 杜威分类法_设计机器人:从都会到休伊,杜威和路易
  4. 储存卡数据怎么恢复?恢复靠它
  5. 【win10专业版】win10系统下Office2013无法激活的解决方法
  6. 用计算机写欧拉恒等式,震惊!计算器里竟然藏着这样一个秘密!
  7. 使用Overleaf写作是参考文献引用没按顺序
  8. [Python]基于pygame的像素转化器
  9. led伏安特性实验误差分析_1实验数据的误差分析与处理.doc
  10. 数据分析之Pandas(三):汇总、统计、相关系数和协方差