interest-picture.component.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { Component, OnInit } from '@angular/core';
  2. import { IonHeader, IonToolbar, IonTitle, IonContent, IonProgressBar, IonButtons, IonBackButton, IonSpinner, IonIcon, IonButton } from '@ionic/angular/standalone';
  3. import { IonTextarea } from "@ionic/angular/standalone";
  4. import { DalleOptions, FmodeChatCompletion, ImagineWork } from 'fmode-ng';
  5. @Component({
  6. selector: 'app-interest-picture',
  7. templateUrl: './interest-picture.component.html',
  8. styleUrls: ['./interest-picture.component.scss'],
  9. standalone: true,
  10. imports: [
  11. IonHeader, IonToolbar, IonTitle, IonContent,
  12. IonProgressBar, IonButtons, IonBackButton,
  13. IonButton, IonTextarea, IonSpinner, IonIcon
  14. ],
  15. })
  16. export class InterestPictureComponent implements OnInit {
  17. userPrompt: string = "";
  18. PictureDescResult: string = '';
  19. imagineWork: ImagineWork | undefined;
  20. images: Array<string> = [];
  21. likedImages: Set<string> = new Set();
  22. constructor() {
  23. this.initializeWork();
  24. }
  25. ngOnInit(): void { }
  26. private initializeWork() {
  27. // 初始化或重置工作状态
  28. this.imagineWork = undefined;
  29. this.images = [];
  30. this.PictureDescResult = '';
  31. }
  32. promptInput(ev: any) {
  33. this.userPrompt = ev.detail.value;
  34. }
  35. async createImage() {
  36. if (!this.userPrompt.trim()) {
  37. this.showToast('请输入创作描述');
  38. return;
  39. }
  40. this.initializeWork();
  41. this.imagineWork = new ImagineWork();
  42. const promptTemplate = `
  43. 您是一名专业的艺术创作顾问,请根据用户的描述,用简练的专业语言重新诠释其创作意图:
  44. ${this.userPrompt}
  45. `;
  46. const completion = new FmodeChatCompletion([
  47. { role: "system", content: "您是一位专业的艺术顾问,擅长理解和优化创作意图。" },
  48. { role: "user", content: promptTemplate }
  49. ]);
  50. completion.sendCompletion().subscribe({
  51. next: (message: any) => {
  52. this.PictureDescResult = message.content;
  53. if (message?.complete) {
  54. this.generateImage();
  55. }
  56. },
  57. error: (error) => {
  58. this.showToast('创作解读失败,请重试');
  59. this.initializeWork();
  60. }
  61. });
  62. }
  63. private generateImage() {
  64. const picturePrompt = `${this.PictureDescResult}\n请根据以上描述创作一幅意境图画。要求:画面构图完整,色彩和谐,风格现代。`;
  65. const options: DalleOptions = {
  66. prompt: picturePrompt,
  67. size: '1024x1024',
  68. };
  69. this.imagineWork?.draw(options).subscribe({
  70. next: (work) => {
  71. if (work?.get("images")?.length) {
  72. this.images = work.get("images");
  73. }
  74. },
  75. error: (error) => {
  76. this.showToast('图片生成失败,请重试');
  77. this.initializeWork();
  78. }
  79. });
  80. }
  81. private async showToast(message: string) {
  82. const toast = document.createElement('ion-toast');
  83. toast.message = message;
  84. toast.duration = 2000;
  85. toast.position = 'top';
  86. document.body.appendChild(toast);
  87. await toast.present();
  88. }
  89. getCurrentTime(): string {
  90. return new Date().toLocaleString('zh-CN', {
  91. month: 'numeric',
  92. day: 'numeric',
  93. hour: 'numeric',
  94. minute: 'numeric'
  95. });
  96. }
  97. async downloadImage(imageUrl: string) {
  98. try {
  99. const response = await fetch(imageUrl);
  100. const blob = await response.blob();
  101. const url = window.URL.createObjectURL(blob);
  102. const a = document.createElement('a');
  103. a.href = url;
  104. a.download = `AI创作_${new Date().getTime()}.png`;
  105. document.body.appendChild(a);
  106. a.click();
  107. window.URL.revokeObjectURL(url);
  108. document.body.removeChild(a);
  109. this.showToast('下载成功');
  110. } catch (error) {
  111. this.showToast('下载失败,请重试');
  112. }
  113. }
  114. async shareImage(imageUrl: string) {
  115. if (navigator.share) {
  116. try {
  117. await navigator.share({
  118. title: 'AI艺术创作',
  119. text: '查看我用AI创作的艺术作品',
  120. url: imageUrl
  121. });
  122. } catch (error) {
  123. this.showToast('分享失败,请重试');
  124. }
  125. } else {
  126. // 复制图片链接
  127. await navigator.clipboard.writeText(imageUrl);
  128. this.showToast('图片链接已复制到剪贴板');
  129. }
  130. }
  131. likeImage(imageUrl: string) {
  132. if (this.likedImages.has(imageUrl)) {
  133. this.likedImages.delete(imageUrl);
  134. } else {
  135. this.likedImages.add(imageUrl);
  136. }
  137. }
  138. isImageLiked(imageUrl: string): boolean {
  139. return this.likedImages.has(imageUrl);
  140. }
  141. async saveToCollection(imageUrl: string) {
  142. // 这里可以添加保存到收藏的逻辑
  143. this.showToast('已添加到收藏');
  144. }
  145. }
  146. // # "AI艺速"项目策划书
  147. // 项目设想
  148. // - 行业:兴趣教育与技能培训领域
  149. // - 用户:对各类新鲜事物抱有三分钟热度、渴望快速入门体验的广大人群
  150. // - 功能:通过程序依据用户感兴趣的内容提供相应的浅显教学步骤及基础指导。