首页 > 其他分享 >代码

代码

时间:2023-12-12 23:01:17浏览次数:23  
标签:String 代码 private public selectedImagePath new void

package com.example;


public class tuxiang extends JFrame {
private static final long serialVersionUID = 1L;
public static final String API_KEY = "TggSZoWYfVLpaGbezMzf2opT";
public static final String SECRET_KEY = "eF9UfmITtR0EUDFkDMyHyCcbKGHe9j8Y";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
private JLabel imageLabel;
private JButton chooseButton;
private JButton processButton;

private String selectedImagePath;

public tuxiang() {
setTitle("Image Processing App");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

initializeComponents();
setLayout(new BorderLayout());

add(imageLabel, BorderLayout.CENTER);
add(chooseButton, BorderLayout.WEST);
add(processButton, BorderLayout.EAST);
}

private void initializeComponents() {
imageLabel = new JLabel();
chooseButton = new JButton("Choose Image");
processButton = new JButton("Process Image");

chooseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chooseImage();
}
});
processButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processImage();
}
});
}
//用来选择文件位置
private void chooseImage() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);

if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
selectedImagePath = selectedFile.getAbsolutePath();
displayImage(selectedImagePath);
}
}

private void displayImage(String imagePath) {
ImageIcon icon = new ImageIcon(imagePath);
Image image = icon.getImage().getScaledInstance(300, 300, Image.SCALE_SMOOTH);
imageLabel.setIcon(new ImageIcon(image));
}

private void processImage() {
if (selectedImagePath != null && !selectedImagePath.isEmpty()) {
try {
String base64Image = getFileContentAsBase64(selectedImagePath, true);
String processedImage = processImageWithBaiduAPI(base64Image);
displayImage(processedImage);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Image processing failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "Please choose an image first.", "Error", JOptionPane.ERROR_MESSAGE);
}
}

private String getFileContentAsBase64(String selectedImagePath, boolean b) {
return selectedImagePath;
}

private String processImageWithBaiduAPI(String base64Image) throws Exception {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "image=" + base64Image);
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();

return new JSONObject(response.body().string()).getString("processed_image");
// Assuming there's a "processed_image" field in the response
}
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();
try {
return new JSONObject(response.body().string()).getString("access_token");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new tuxiang().setVisible(true);
}
});
}
}

标签:String,代码,private,public,selectedImagePath,new,void
From: https://www.cnblogs.com/mine-my/p/17898063.html

相关文章

  • WSL更新失败(退出代码: 1603) - Error code: Wsl/CallMsi/E_ABORT
    Whathappened?WSL莫名其妙的更新了,完成更新以后莫名奇妙地启动不起来了。每次运行WSL的时候都会给我提示WSL正在完成升级...更新失败(退出代码:1603)。Errorcode:Wsl/CallMsi/E_ABORT抓耳挠腮找了半天,我甚至不管写了一半的代码和笔记,把WSL卸载后重装,结果居然无法启动......
  • 数据分享|R语言用RFM、决策树模型顾客购书行为的数据预测|附代码数据
    全文链接:http://tecdat.cn/?p=30330最近我们被客户要求撰写关于RFM、决策树模型的研究报告,包括一些图形和统计输出。团队需要分析一个来自在线零售商的数据该数据包含了78周的购买历史。该数据文件中的每条记录包括四个字段。客户的ID(从1到2357不等),交易日期,购买的书籍数量,以及......
  • Facebook群发脚本常用代码分享!
    一、引言在数字时代,社交媒体已成为商业和个人通信的重要渠道,Facebook作为全球最大的社交网络平台之一,为企业和个人提供了广泛的沟通工具。其中,群发消息功能是许多用户在营销和社交活动中常用的功能,本文将分享一些常用的Facebook群发脚本代码,帮助您实现更高效的社交媒体营销。二、Fa......
  • 代码改进的方法
    今日通过对代码编译报错原因的分析,通过阅读代码大全后,总结了以下几个代码改进的方法,具体方法如下:以下是我代码编译报错改进的方法:代码改进,这部分包括以下几个主题,协同、测试、调试、重构、调整。这里的测试是开发者测试,应该是指单元测试了,现有很多合适的工具,比如NUnit、CppUnit......
  • 《代码大全》观后感
    通过这几天对《代码大全》的阅读有了以下自己的感悟与体会。具体体会与感悟入下:《代码大全2》是一本非常经典的软件开发书籍。在书中,强调了比较优秀的代码结构和命名规范的重要性。书中注释的部分帮助我理解怎么去编写有意义的注释,合适的注释可以提供代码理解上的便利,但是过多......
  • 代码编译出错可能的原因
    今天总结了一下代码编译报错可能的情况。原因如下有以下几种可能:错误处理程序1.处理预料中可能要发生的错误,在程序的正确性与健壮性间平衡;2.方法:返回中立值、换用下一个正确的数据、返回与前次相同的数据、换用最接近的合法值、把警告信息记录到日志文件中、返回一个错误码、......
  • 存代码
    [USACO17OPEN]BovineGenomicsG#include<bits/stdc++.h>#defineintlonglong#definemaxm0X66CCFF#defineN510namespaceIO{inlinevoidclose(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);}inlinevoidFire(){freopen(".in......
  • shell脚本模板-从git拉取代码并打包部署
    source/etc/profile.~/.bash_profile#拉取能耗后端代码cd/usr/local/testmvncleanecho-e"从git华为云拉取后端代码"#首次clonegitpulltest.gitecho-e"从git华为云代码拉取完成"#工程打包echo-e"开始打jar包"mvnpackage-Dmaven.test.skip=true#删除原来的jar包rm......
  • Amazon CodeWhisperer 审查:最新的 AI 代码伴侣
    最近,亚马逊云科技宣布了一项机器学习支持的服务,该服务通过根据开发人员在自然语言中的评论和他们在集成开发环境中的代码生成代码建议来帮助提高开发人员的工作效率。这项名为AmazonCodeWhisprer 的服务仍处于预览阶段,可以免费使用。这项服务类似于微软去年推出的GitHubcopi......
  • 多线程爬虫抓取京东运行流程-大公司抢着要代码
    之前有个大公司找我,需要爬取京东有关行业商家的价格信息做对比,方便后期自己的产品定位以及舆情监控,让我写一个通用的爬虫模版,方便他们那边技术调整修改,于是带着这样的问题,我给了他们一些几点建议。首先,你需要安装必要的库,包括HTTP库、JSON库、爬虫库、代理库和可视化库。可以使......