【问题标题】:return origin color values of a picture返回图片的原始颜色值
【发布时间】:2018-11-09 03:37:41
【问题描述】:

我正在做一个关于在 java 中编辑图片的任务。该作业要求编写一个代码,将图像的颜色更改为 3 种不同的阴影并对其进行动画处理,使其像 GIF 一样。我完成了这些任务,但不知何故,当它在阴影之间变化时,图片会失去细节,在第三次阴影变化时只留下蓝色的空白。我尝试在更改下一个之前添加一个代码将颜色值返回到原点,但仍然会发生此问题。可能是我弄错了代码。有人可以帮我解决这个问题吗?

/* Assignment 3, Part 1 - Go Psychedelic! */

public class Assignment3Part1
{
//
  public static void main(String [] args) throws InterruptedException
  {
    String filename;
    if (args.length > 0) {
      // got a filename passed into program as a parameter
      // don't change this part of the code needed by TA for grading
      filename = args[0];      
      System.out.println("Filename passed in: " + filename);

    } else {
      // ask user for a picture
      filename = FileChooser.pickAFile();
      System.out.println("User picked file: " + filename);
    }
    Picture pic = new Picture(filename);   // Picture to modify
    //
    pic.show();             // Show the original picture
    Thread.sleep(1000);     //  Pause for 1 second.  You can pause for less if you like

    // TODO: insert method call to tint your picture
    pic.tintRed(50);

    pic.repaint();          // Show the tinted picture
    Thread.sleep(1000);     //  Pause for 1 second

    // TODO: insert method call to tint your picture
    pic.tintRed(-50);
    pic.tintGreen(20);

    pic.repaint();          // Show the tinted picture
    Thread.sleep(1000);     //  Pause for 1 second

    // TODO: insert method call to tint your picture
    pic.tintGreen(0);
    pic.tintBlue(10);

    pic.repaint();          // Show the tinted picture
    Thread.sleep(1000);     //  Pause for 1 second

  }  // End of main method

}  // End of class

方法代码

  public void tintRed(int percent)
  {
    Pixel[] pixelArray = this.getPixels();
    Pixel pixel = null;
    int value = 0;
    int i = 0;

    //loop through all the pixels in the array
    while (i<pixelArray.length)
    {
      // get the current pixel
      pixel = pixelArray[i];
      // get the value
      value = pixel.getRed();
      // set the value to % of what it was
      pixel.setRed((int)(value*percent));
      // increment the index
      i++;
    }
  }

蓝色和绿色值相同

【问题讨论】:

    标签: java image colors


    【解决方案1】:

    您正在使颜色饱和到所有内容都是 100% 的颜色(一张空白图片)。你必须让你的着色算法变得更温和:比如 25%、50%、75%

    【讨论】:

    • 我有点明白你的意思。正如我所看到的,问题在于百分比值。我只是在其中添加了一行,所以它现在类似于 value*(percentage/100)。是的,它为任务带来了更准确的结果。但现在 tintRed 不能以某种方式工作,哈哈。你也可以帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2020-01-19
    • 2021-05-02
    • 1970-01-01
    • 2020-02-05
    相关资源
    最近更新 更多