百度AI,实现图像增强,Base64 图片转化问题

本文主要是结合了几个技术,整合一下,根据自己的需求,实现了图像增强
首先需要用到的是百度AI技术,使用的技术是引入SDK包,这个可以调用里面Jar包的现成方法,不用自己编写图像增强方法。
可以具体参考如下官方技术文档连接
图像增强技术文档链接
需要先注册个百度云账户,登陆成功之后,点击控制台,出现如下图片,选择图像增强(百度AI其他功能类似)
在这里插入图片描述
然后点击创建应用,这个步骤很关键,因为创建成功后,有三个关键参数 AppID API Key Secret Key。
如下图所示

在这里插入图片描述接下来就是下载SDK包,引入依赖,复制粘贴百度AI的sample代码,根据自己的需求进行更改。
我的代码如下:

public class MagicSample {
	    public static final String APP_ID = "你的APP_ID";
	    public static final String API_KEY = "你的API_KEY";
	    public static final String SECRET_KEY = "你的SECRET_KEY";

	     public static void main(String[] args)  {
	        // 初始化一个AipImageProcess
	        AipImageProcess client1 = new AipImageProcess(APP_ID, API_KEY, SECRET_KEY);

	        // 可选:设置网络连接参数
//	        client.setConnectionTimeoutInMillis(2000);
//	        client.setSocketTimeoutInMillis(60000);

	        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//	        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//	        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

//	        // 调用接口
//	        String path = "test.jpg";
//	        JSONObject res = client.imageProcess(path, new HashMap<String, String>());
//	        System.out.println(res.toString(2));
	        HashMap<String, String> options = new HashMap<String, String>();
	        
	        
	        // 参数为本地路径
	        String image = "G:\\fig1.jpg";   //这里是你本地保存的待处理照片地址
	        JSONObject res = client1.contrastEnhance(image, options);
	        System.out.println(res.toString(2));
//
//	        // 参数为二进制数组
//	        byte[] file = getFile("test.jpg");
//	        res = client1.contrastEnhance(file, options);
//	        System.out.println(res.toString(2));
//	        
	    }
}

自上面一步,运行成功,会生成一串Base64 的String 参数,包含了图像增强后的信息,这个时候需要用到Base64的方法,进行解码操作,生成图片保存到本地,我这里参考了如下的文章
该处参考的文章链接
又因为现在Base64更新问题,encoder和decoder方法引入的包发生变化了,以及方法实例化有一些细微差别,需要修改,这里参考了如下的文章
该处参考的文章链接
最终修改得到代码,可以参考参考

public class BaseConvert {
	   /**
     * 图片转化成base64字符串
     * @param imgPath
     * @return
     */
    public static String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        String imgFile = imgPath;// 待处理的图片
        InputStream in = null;
        byte[] data = null;
        String encode = null; // 返回Base64编码过的字节数组字符串
        // 对字节数组Base64编码
        Encoder encoder =Base64.getEncoder();
        try {
            // 读取图片字节数组
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            encode = encoder.encodeToString(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return encode;
    }
    @SuppressWarnings("finally")
    public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片
        if (imgData == null) // 图像数据为空
            return false;

         Decoder decoder = Base64.getDecoder();

     //   BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try {
            out = new FileOutputStream(imgFilePath);
            // Base64解码
            byte[] b = decoder.decode(imgData);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
            return true;
        }
    }
}

再下一步,就是用前面生成增强后的Base64 字符串和本地保存位置传入GenerateImage方法中,进行测试。

String imageStr ="Base64 字符串"
BaseConvert.GenerateImage(imageStr, "G://fig2.jpg");

最后大功告成,打开G盘的文件夹,查看两者对比。
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值