Klasse 1 importieren java.util.Scanner;Ein Bild drehen GreyScale
public class Image {
public static void main(String[] args) {
String filename = "test.ppm";
TestClass img = new TestClass(filename);
StdDraw.setCanvasSize(img.getWidth(), img.getHeight());
StdDraw.setXscale(0, img.getWidth());
StdDraw.setYscale(0, img.getHeight());
Scanner keyboard = new Scanner(System.in);
img.display();
System.out.println("Type anything then enter");
keyboard.next();
TestClass modified = new TestClass(filename);
modified.removeReds();
modified.display();
}
}
Klasse 2
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestClass {
private int width;
private int height;
private Color[][] data;
public TestClass(String filename) {
Scanner fileIn = null;
try {
fileIn = new Scanner(new FileInputStream(filename));
} catch (FileNotFoundException e) {
System.out.println("File not found");
System.out.println("Looked in " +
System.getProperty("user.dir"));
System.exit(1);
}
// Header information
String format = fileIn.nextLine();
width = fileIn.nextInt();
height = fileIn.nextInt();
int colorDepth = fileIn.nextInt();
System.out.println("File: " + filename + " is " + width
+ " x " + height + " pixels");
// Verify file format
if (!format.equals("P3") || colorDepth != 255) {
System.out.println("Unknown format for file");
System.exit(1);
}
// Read in data pixel by pixel
data = new Color[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int red = fileIn.nextInt();
int green = fileIn.nextInt();
int blue = fileIn.nextInt();
data[i][j] = new Color(red, green, blue);
}
}
}
public void display() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
StdDraw.setPenColor(data[i][j]);
StdDraw.point(j, height - i - 1);
}
}
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void removeReds() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int red = data[i][j].getRed();
int green = data[i][j].getGreen();
int blue = data[i][j].getBlue();
int sum = red+green+blue;
Color newColor = new Color(sum/3);
}
}
}
}
Also muss ich dieses Bild Graustufen turnn. Ich weiß, dass du das rote Grün und Blau durchpflügen musst. Ich habe versucht, aber wenn jeder ich das Programm laufen lassen ändert es nur die Farben, die sie ein wenig dunkler
Mögliche Duplikate von [Umwandlung von RGB in Graustufen/Intensität] (http://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intness) – Jeremy
Ich glaube nicht, ich habe ein Duplikat – jackjackk1223