06.入门篇-AI编程助手

6 AI编程助手

6.1 常见 AI 编程助手

编程助手 公司 支持的IDE 地址 备注
GitHub Copilot 微软+OpenAl VS Code、JetBrains、Visual Studio等 https://github.com/features/copilot  
Amazon Q 亚马逊 VS Code、JetBrains、Visual Studio等 https://aws.amazon.com/cn/q/developer/  
星斗 黑马程序员 JetBrains等 https://t.zsxq.com/daty0 有程序题
文心快码 百度 VS Code、JetBrains、Visual Studio等 https://comate.baidu.com/zh  
通义灵码 阿里巴巴 VS Code、JetBrains等 https://tongyi.aliyun.com/lingma 演示使用
CodeGeeX 智谱Al VS Code、JetBrains等 https://codegeex.cn/zh-CN  
MarsCode 字节跳动 VS Code、JetBrains等 https://www.marscode.cn/  

 

 

 

 

 

 

 

 

 

6.2 安装 AI 插件

点击设置按钮 -> Plugins -> Marketplace -> 搜索框输入 TONGYILingma(阿里巴巴公司) -> Install

在 Installed 中可以查看到已经安装过的插件有 TONGYILingma ,可以在此处点击 Disable禁用或 Uninstall卸载插件

安装成功后,在 IDE 的右侧边栏消息(小铃铛下面)出现一个标识,点击使用,首次使用需要登录

6.3 AI 插件使用

1.在对话框中输入自己的需求,与通义千问对话相似,AI 以对话的形式输出代码,可以复制粘贴到项目中

2.在所写项目中的代码行尾回车,AI 智能书写接下来可能想要写的代码

3.在方法的代码块上有标识,点击可以解释代码、单元测试等

6.4 AI 案例

需求:利用AI编程助手开发一个程序,可以将任意图片转为字符画保存

请帮我实现一个Java程序,可以将用户指定的图片转为字符画保存到用户指定的文件

1.创建com.itheima.ai 包 Package

2.创建与类名相同的文件名 ImageToAsciiArt

3.粘贴代码并运行,按照提示要求输入

package com.itheima.ai;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * 图片转字符画工具类
 */
public class ImageToAsciiArt {

    // 字符集,从暗到亮排列
    private static final String ASCII_CHARS = "@%#*+=-:. ";

    /**
     * 将图片转换为字符画并保存到文件
     *
     * @param imagePath 图片路径
     * @param outputPath 输出文件路径
     * @param width 字符画宽度(字符数)
     * @param height 字符画高度(字符数)
     * @throws IOException 文件读写异常
     */
    public static void convertImageToAscii(String imagePath, String outputPath, int width, int height)
            throws IOException {
        // 读取图片
        BufferedImage originalImage = ImageIO.read(new File(imagePath));

        // 调整图片大小
        BufferedImage resizedImage = resizeImage(originalImage, width, height);

        // 转换为字符画
        StringBuilder asciiArt = new StringBuilder();
        for (int y = 0; y < resizedImage.getHeight(); y++) {
            for (int x = 0; x < resizedImage.getWidth(); x++) {
                Color pixelColor = new Color(resizedImage.getRGB(x, y));
                // 计算灰度值
                int grayValue = (int) (pixelColor.getRed() * 0.299 + pixelColor.getGreen() * 0.587
                        + pixelColor.getBlue() * 0.114);
                // 映射到字符
                int charIndex = (grayValue * (ASCII_CHARS.length() - 1)) / 255;
                asciiArt.append(ASCII_CHARS.charAt(charIndex));
            }
            asciiArt.append("\n");
        }

        // 保存到文件
        try (FileWriter writer = new FileWriter(outputPath)) {
            writer.write(asciiArt.toString());
        }
    }

    /**
     * 调整图片大小
     *
     * @param originalImage 原始图片
     * @param targetWidth 目标宽度
     * @param targetHeight 目标高度
     * @return 调整后的图片
     */
    private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        g.dispose();
        return resizedImage;
    }

    /**
     * 主程序入口
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            // 获取用户输入
            System.out.print("请输入图片文件路径: ");
            String imagePath = scanner.nextLine();

            System.out.print("请输入输出文件路径: ");
            String outputPath = scanner.nextLine();

            System.out.print("请输入字符画宽度(建议50-100): ");
            int width = Integer.parseInt(scanner.nextLine());

            System.out.print("请输入字符画高度(建议30-50): ");
            int height = Integer.parseInt(scanner.nextLine());

            // 执行转换
            convertImageToAscii(imagePath, outputPath, width, height);

            System.out.println("字符画已成功保存到: " + outputPath);

        } catch (IOException e) {
            System.err.println("文件操作错误: " + e.getMessage());
        } catch (NumberFormatException e) {
            System.err.println("请输入有效的数字");
        } catch (Exception e) {
            System.err.println("发生错误: " + e.getMessage());
        } finally {
            scanner.close();
        }
    }
}

------------------------------------------------ 执行后
D:\Software\jdk17\bin\java.exe "-javaagent:D:\Software\JetBrainsIntelliJ IDEA 2025.2.4\lib\idea_rt.jar=1212" -Dfile.encoding=UTF-8 -classpath D:\Software\JavaCode\p1-basic\out\production\p1-basic com.itheima.ai.ImageToAsciiArt
请输入图片文件路径: D:\360MoveData\Users\马俊南\Desktop\1.jfif
请输入输出文件路径: D:\360MoveData\Users\马俊南\Desktop\1.txt
请输入字符画宽度(建议50-100): 60
请输入字符画高度(建议30-50): 80
字符画已成功保存到: D:\360MoveData\Users\马俊南\Desktop\1.txt

Process finished with exit code 0

图片展示:

1

图片转为的字符画展示:

                                                            
                            ..... ..                        
                       . .*@@%%%@%=...                      
            .....   ..=%.  .@++++++++%-.                    
           .@###.  .%.     ..+++++++++++%..                 
           -#-.#%..@..     .%+++++++++++++%                 
          .%#####@...      .++++++++++++++++.               
           -##### .%..    .++++++++++++++++++=.             
           .@##-%.     . %++++++++++++++++++++#.            
           .....%.    .#.*+++++++++++++++++++++*            
     ...    .#. .@..-@.. .@+++++++++++++++++++++:           
     %  .@ .%.    ..       .+++++++++++++++++++++..         
   .+.  .....      ... ......++++++++++++++++++++%.         
   .:     @.      .::::--+*#@%%*++++++++++++++++++-.        
   ..     :.                  .++++++++++++++++++++.        
   ..                         .@+++++++++++++++++++%        
   .%                         ..*+++++++++++++++++++.       
    ..           -%.            .+++++++++++++++++++.       
   .-             ..:%...       .@++++++++++++++++++%       
   #                  .=+.       .+++++++++++++++++++       
  .@                     .@      .#++++++++++++++++++..     
  .*.          .=.         .=-.  .=++++++++++++++++++.      
   .-  .         %            ..  .+++++++++++++++++++      
     .-.          =..             .*+++++++++++++++++#      
       .           ..             .%+++++++++++++++++%      
       .            .-            .%+++++++++++++++++#      
        .            .#            *+++++++++++++++++=      
        .             .%           ++++++++++++++++++.      
        %              .%.         *+++++++++++++++++       
        -                %.       .%++++++++++++++++*       
         .                        .%++++++++++++++++%.      
        .@                        .*++++++++++++++++.       
         .                        .++++++++++++++++#.       
         .*                      ..++++++++++++++++.        
          ..                     .%+++++++++++++++%         
           %.                    .+++**+++++++++++.         
          ..%.                  .*+#... %++++++++.          
            .=.                 .+:     ...%++++#.          
            ..-                 @+..     @ .+++-.           
              #@:...           .@.      .*  @*..            
              @####%*:.........%.       .*  =.              
              =@##########%@@##%.       .%..#               
              @++*@%###########%.       ....%               
              @+++++++++++++++++@.      %  .@..             
              %++++++++++++++++++#.    .. .%++%.            
              #+++++++++++++++++++:  .... .++++.            
              %+++++++++++++++++++@  :.  .#++++: .@:-%      
              %++++++++++++++++++++#=....*+++++....  .% .   
              @++++++++++++++++++%@.*+#*+++++++..      .    
              %+++++++++++++++++%%...+++++++++*+=      #    
              =++++++++++++++++@. ...%%++++++%++%.     @    
              .++++++++++++++*-.#%..  ..+@%%++++*.     #    
              .++%+++++++++*@+.         ...%++++*.     %    
              .*++..+@%%%-...           ...#++++%      @    
               @++. ..                  @..+++++@.    .:    
               .++:  .                . ..:+++++=    ...    
               .++%. #                .@. *+++++.     *.    
               .#++. .=              .%  %+++++*     ..     
                -++#. .*.           .%  =++++++#.    %      
                .+++@. .+...    ...%...++++++++.   ...      
                .%+++@.. .:@*=+%*.. ..@+++*%%+..   ..       
                ..++++++.  .....  ..@++#..    .*..+.        
                 .%++++++%+.....+@++++@.. .     ..          
                  .++++++++++++++++++..*#%..                
                  .@++++++++++++++++#..####.                
                   .++++++++++++++++. %####:                
                    @++++++++++++++*  .####                 
                    .*+++++++++++++.. .@##%                 
                    ..+++++++++++++ . .....                 
                     +@+++++++++++*%.                       
                   ... #*+++++++++: %.                      
                    #.....+@#**%*.  .=                      
                    .                #                      
                    %                %                      
                    . .             .:                      
                     #.             #.                      
                    . .+..      ...%                        
                         #%.....%*                          
                         . ......                           
                                                            

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2025-11-27 17:31  马俊南  阅读(12)  评论(0)    收藏  举报