首页 > 其他分享 >12月6每日打卡

12月6每日打卡

时间:2023-12-06 22:22:05浏览次数:28  
标签:body 12 java String 每日 application new import 打卡

实验二:百度图像增强与特效SDK实验

一、实验要求

 任务一:下载配置百度图像增强与特效的Java相关库及环境(占10%)。

    任务二:了解百度图像增强与特效相关功能并进行总结(占20%)。

    任务三:完成图像增强GUI相关功能代码并测试调用,要求上传自己的模糊照片进行图像增强(占30%)。

    任务四:完成图像特效GUI相关功能代码并测试调用,要求上传自己的照片进行图像特效(占30%)。

实验总结:(占10%)

二、实验步骤

任务一:

 

任务二:

百度图像增强与特效是百度提供的一项人工智能服务,它可以通过应用各种图像处理算法对图像进行增强和添加特效,以改善图像的质量和视觉效果。以下两种是我用到百度图像增强与特效功能的总结:

  1. 图像增强:百度图像增强服务提供了多种图像增强技术,包括亮度增强、对比度增强、锐化等。这些技术可以使图像更清晰、更鲜明,并提高图像的可视性。
  2. 图片动漫化:图片动漫化是一种将真实照片或图像转换成类似动漫风格的艺术效果的过程。这种效果通常包括对图像进行线条加粗、颜色处理和光影效果等,使图像看起来像是从动漫或漫画中提取出来的。

任务三:

 

MainGUI.java

package baidu.com;

 

import okhttp3.*;

import org.json.JSONObject;

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.net.URLEncoder;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Base64;

 

public class MainGUI extends JFrame {

    private JButton button;

    private JTextArea textArea;

    private JLabel imageLabel;

 

    public MainGUI() {

        setTitle("图片处理GUI");

        setSize(600, 400);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

        // 创建按钮和文本区域

        button = new JButton("处理图片");

        textArea = new JTextArea();

        textArea.setEditable(false);

 

        // 创建图片标签

        imageLabel = new JLabel();

        imageLabel.setPreferredSize(new Dimension(300, 300));

 

        // 添加组件到窗口布局中

        Container container = getContentPane();

        container.setLayout(new BorderLayout());

        container.add(button, BorderLayout.NORTH);

        container.add(textArea, BorderLayout.CENTER);

        container.add(imageLabel, BorderLayout.WEST);

 

        // 添加按钮点击事件处理程序

        button.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                try {

                    // 调用Sample类方法获取图片Base64编码

                    String base64Image = Sample.getFileContentAsBase64("D:\\3.jpg", true);

                    textArea.setText("Base64编码:" + base64Image);

 

                    // 处理图片并显示

                    processAndDisplayImage(base64Image);

                } catch (IOException ex) {

                    ex.printStackTrace();

                }

            }

        });

    }

 

    private void processAndDisplayImage(String base64Image) throws IOException {

        // 请求处理图片

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image=" + base64Image);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance?access_token=" + getAccessToken())

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .addHeader("Accept", "application/json")

                .build();

        Response response = Sample.HTTP_CLIENT.newCall(request).execute();

 

        // 获取处理后的图片Base64编码

        String processedImageBase64 = new JSONObject(response.body().string()).getString("image");

 

        // 显示处理后的图片

        showImage(processedImageBase64);

    }

 

    private void showImage(String base64Image) {

        // 解码Base64编码为字节数组

        byte[] imageBytes = Base64.getDecoder().decode(base64Image);

 

        // 创建图像对象

        ImageIcon imageIcon = new ImageIcon(imageBytes);

        Image image = imageIcon.getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT);

        imageIcon = new ImageIcon(image);

 

        // 更新图片标签的显示

        imageLabel.setIcon(imageIcon);

    }

 

    private String getAccessToken() throws IOException {

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + Sample.API_KEY

                + "&client_secret=" + Sample.SECRET_KEY);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/oauth/2.0/token")

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .build();

        Response response = Sample.HTTP_CLIENT.newCall(request).execute();

        return new JSONObject(response.body().string()).getString("access_token");

    }

 

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override

            public void run() {

                MainGUI mainGUI = new MainGUI();

                mainGUI.setVisible(true);

            }

        });

    }

}

Sample.java

package baidu.com;

 

import okhttp3.*;

import org.json.JSONObject;

 

import java.io.*;

 

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Base64;

import java.net.URLEncoder;

 

class Sample {

    public static final String API_KEY = "0pMg2OjMvVjVW0uSq3QxSc6l";

    public static final String SECRET_KEY = "k63PimOyzKMLU9yVPDx7jxbWb8dIPT0j";

 

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

 

    public static void main(String []args) throws IOException{

//      booleanture = true;

        String filepath ="D:\\2.jpg";

        String name= getFileContentAsBase64(filepath,true);

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        // image 可以通过 getFileContentAsBase64("C:\fakepath\1.png.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true

        RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image="+name);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + getAccessToken())

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .addHeader("Accept", "application/json")

                .build();

        Response response = HTTP_CLIENT.newCall(request).execute();

        System.out.println(response.body().string());

 

    }

 

    /**

     * 获取文件base64编码

     *

     * @param path      文件路径

     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true

     * @return base64编码信息,不带文件头

     * @throws IOException IO异常

     */

    static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {

        byte[] b = Files.readAllBytes(Paths.get(path));

        String base64 = Base64.getEncoder().encodeToString(b);

        if (urlEncode) {

            base64 = URLEncoder.encode(base64, "utf-8");

        }

        return base64;

    }

 

 

    /**

     * 从用户的AK,SK生成鉴权签名(Access Token)

     *

     * @return 鉴权签名(Access Token)

     * @throws IOException IO异常

     */

    static String getAccessToken() throws IOException {

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY

                + "&client_secret=" + SECRET_KEY);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/oauth/2.0/token")

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .build();

        Response response = HTTP_CLIENT.newCall(request).execute();

        return new JSONObject(response.body().string()).getString("access_token");

    }

 

}

 

任务

 

MainGUI.Java

package baidu.com;

 

import okhttp3.*;

import org.json.JSONObject;

 

import java.io.*;

 

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Base64;

import java.net.URLEncoder;

 

class Sample {

    public static final String API_KEY = "0pMg2OjMvVjVW0uSq3QxSc6l";

    public static final String SECRET_KEY = "k63PimOyzKMLU9yVPDx7jxbWb8dIPT0j";

 

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

 

    public static void main(String []args) throws IOException{

//      booleanture = true;

        String filepath ="D:\\2.jpg";

        String name= getFileContentAsBase64(filepath,true);

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        // image 可以通过 getFileContentAsBase64("C:\fakepath\1.png.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true

        RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image="+name);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + getAccessToken())

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .addHeader("Accept", "application/json")

                .build();

        Response response = HTTP_CLIENT.newCall(request).execute();

        System.out.println(response.body().string());

 

    }

 

    /**

     * 获取文件base64编码

     *

     * @param path      文件路径

     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true

     * @return base64编码信息,不带文件头

     * @throws IOException IO异常

     */

    static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {

        byte[] b = Files.readAllBytes(Paths.get(path));

        String base64 = Base64.getEncoder().encodeToString(b);

        if (urlEncode) {

            base64 = URLEncoder.encode(base64, "utf-8");

        }

        return base64;

    }

 

 

    /**

     * 从用户的AK,SK生成鉴权签名(Access Token)

     *

     * @return 鉴权签名(Access Token)

     * @throws IOException IO异常

     */

    static String getAccessToken() throws IOException {

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY

                + "&client_secret=" + SECRET_KEY);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/oauth/2.0/token")

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .build();

        Response response = HTTP_CLIENT.newCall(request).execute();

        return new JSONObject(response.body().string()).getString("access_token");

    }

 

}

Sample.java

package baidu.com;

 

import okhttp3.*;

import org.json.JSONObject;

 

import java.io.*;

 

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Base64;

import java.net.URLEncoder;

 

class Sample {

    public static final String API_KEY = "0pMg2OjMvVjVW0uSq3QxSc6l";

    public static final String SECRET_KEY = "k63PimOyzKMLU9yVPDx7jxbWb8dIPT0j";

 

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

 

    public static void main(String []args) throws IOException{

//      booleanture = true;

        String filepath ="D:\\2.jpg";

        String name= getFileContentAsBase64(filepath,true);

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        // image 可以通过 getFileContentAsBase64("C:\fakepath\1.png.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true

        RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image="+name);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + getAccessToken())

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .addHeader("Accept", "application/json")

                .build();

        Response response = HTTP_CLIENT.newCall(request).execute();

        System.out.println(response.body().string());

 

    }

 

    /**

     * 获取文件base64编码

     *

     * @param path      文件路径

     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true

     * @return base64编码信息,不带文件头

     * @throws IOException IO异常

     */

    static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {

        byte[] b = Files.readAllBytes(Paths.get(path));

        String base64 = Base64.getEncoder().encodeToString(b);

        if (urlEncode) {

            base64 = URLEncoder.encode(base64, "utf-8");

        }

        return base64;

    }

 

 

    /**

     * 从用户的AK,SK生成鉴权签名(Access Token)

     *

     * @return 鉴权签名(Access Token)

     * @throws IOException IO异常

     */

    static String getAccessToken() throws IOException {

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");

        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY

                + "&client_secret=" + SECRET_KEY);

        Request request = new Request.Builder()

                .url("https://aip.baidubce.com/oauth/2.0/token")

                .method("POST", body)

                .addHeader("Content-Type", "application/x-www-form-urlencoded")

                .build();

        Response response = HTTP_CLIENT.newCall(request).execute();

        return new JSONObject(response.body().string()).getString("access_token");

    }

 

}

 

标签:body,12,java,String,每日,application,new,import,打卡
From: https://www.cnblogs.com/yunbianshangdadun/p/17880680.html

相关文章

  • 【CCFCSP】2212真题笔记
    -1.现值计算分析做第一题避免用vector,会把简单问题复杂化普通数组或者哈希映射就足够解决问题了微微微模拟,题目有公式ans(-14.059)=(-200)x(1.05^0)+100x(1.05^-1)+100x(1.05^-2)测试数据:20.05-200100100AC:#include<bits/stdc++.h>usingnamespacestd......
  • 闲话12.6
    换了一个拉格兰头像。做了做化学生物的样卷,生物样卷91pts,化学样卷95pts,赢!物理明天再做......
  • CCF202112-1 序列查询~~~(极简代码)
    问题描述: 解题思路:#include<iostream>usingnamespacestd;intshumu,yushuan,l,r,ans;intset=1;intmain(){ cin>>shumu>>yushuan; cin>>l; while(cin>>r) { ans+=(r-l)*set; l=r; set++; } cout<<......
  • skywalking 部署安装 https://blog.csdn.net/swg321321/article/details/129704345
    https://blog.csdn.net/swg321321/article/details/129704345 前言在分布式系统中会出现服务间的相互调用,且服务数量众多。一般会出现如下异常请求出现异常,需要定位定位具体是哪个服务器发生异常,需要对这个请求链路一步一步调试才能确定那个服务出现异常。出现定位异常服务难......
  • 第12章. 映射(Map)
    映射(Map)Map在有些变成语言中也叫作字典(比如在Python中)Map的每一个Key是唯一的,Value可以不是唯一的Map中的每一个Key对应一个Value一、Map的接口设计publicinterfaceMap<K,V>{intsize;booleanisEmpty();voidclear();Vput(Kkey,......
  • 每日总结
    今天看了设计模式中的桥接模式和适配器模式。桥接模式包含以下角色:抽象类,扩充抽象类,实现接口,具体实现类。适配器模式包含以下角色:目标抽象类,适配器类,适配者类,客户类。适配器模式主要是将一个类的接口转化为用户希望的另一个接口,使原本的由于接口不兼容而不能一起工作的那些类可......
  • 12/6每日总结
    今天继续学习了注意力机制,尤其学了添加了attention的seq2seq模型,还学习了自注意力机制,理解了attention机制的精髓使用注意力机制的seq2seq之前提到使用两个循环神经网络的编码器解码器结构实现了seq2seq的学习,实现机器翻译的功能循环神经网络编码器将可变序列转换为固定形状的上......
  • 每日总结12.6
    百度图像增强与特效SDK实验今天在完成实验的过程中遇到了一个问题,通过调用接口生成的图片格式为base64格式需要转化为图片格式,下面为转码的部分类:packagecom.baidu.test;importjava.io.*;importjava.util.Base64;publicclassImageUtils{publicstaticbool......
  • 12.6每日总结
    今天进行了软件构造的实验二,实验二:百度图像增强与特效SDK实验(2023.12.6日完成)    任务一:下载配置百度图像增强与特效的Java相关库及环境(占10%)。    任务二:了解百度图像增强与特效相关功能并进行总结(占20%)。    任务三:完成图像增强GUI相关功能代码并测试调用,要求上......
  • 不再支持安卓 7.0!谷歌Chrome浏览器120稳定版发布
    今天,谷歌面向所有支持平台,发布了Chrome浏览器的120稳定版更新,该版本不再支持安卓7.0/7.1系统。在新功能上,Chrome浏览器120稳定版推出一项新的“主动安全检查”功能,该功能会定期运行,检查浏览器是否存在安全问题,并在检查完成之后通知用户。在设置中也为此新增了一个新的“安全检......