2016-05-27 5 views
0

Ich habe ein Programm, das Kantenerkennung bei Bildern ausführt, die ich in Java lade. Wie kann ich das verarbeitete Bild, das Java zurückgibt, in eine JPG-Datei umwandeln und speichern?Wie kann ich ein Picture-Objekt in eine JPG-Datei in Java verwandeln, damit ich es speichern kann?

Momentan verwandelt das Programm jedes Bild in ein Bildobjekt. Die Kantenerkennung arbeitet an diesem Bildobjekt und gibt dann das verarbeitete Bild zurück. Ich habe die Hauptklasse unten und kann die Klasse hochladen, die das Bildobjekt definiert, wenn sie angefordert wird. Ich würde gerne in der Lage sein, das "Schwan" -Objekt hier zu nehmen, es in eine JPG-Datei umzuwandeln und dann zu speichern.

import java.util.Scanner; 

public class PictureTester 
{ 
    private static Scanner input = new Scanner(System.in); 

    public static void testEdgeDetection2() 
    { 
    Picture swan = new Picture("clone_1_water-timelapse_t0.jpg"); 
    swan.edgeDetection2(10); 
    swan.explore(); 
    } 

    public static void main(String[] args) 
    { 

    testEdgeDetection2(); 

    } 
} 

Quellcode für Bild:

import java.awt.*; 
import java.awt.font.*; 
import java.awt.geom.*; 
import java.awt.image.BufferedImage; 
import java.text.*; 
import java.util.*; 
import java.util.List; // resolves problem with java.awt.List and java.util.List 

/** 
* A class that represents a picture. This class inherits from 
* SimplePicture and allows the student to add functionality to 
* the Picture class. 
* 
*/ 
public class Picture extends SimplePicture 
{ 
    ///////////////////// constructors ////////////////////////////////// 

    /** 
    * Constructor that takes no arguments 
    */ 
    public Picture() 
    { 
    /* not needed but use it to show students the implicit call to super() 
    * child constructors always call a parent constructor 
    */ 
    super(); 
    } 

    /** 
    * Constructor that takes a file name and creates the picture 
    * @param fileName the name of the file to create the picture from 
    */ 
    public Picture(String fileName) 
    { 
    // let the parent class handle this fileName 
    super(fileName); 
    } 

    /** 
    * Constructor that takes the width and height 
    * @param height the height of the desired picture 
    * @param width the width of the desired picture 
    */ 
    public Picture(int height, int width) 
    { 
    // let the parent class handle this width and height 
    super(width,height); 
    } 

    /** 
    * Constructor that takes a picture and creates a 
    * copy of that picture 
    * @param copyPicture the picture to copy 
    */ 
    public Picture(Picture copyPicture) 
    { 
    // let the parent class do the copy 
    super(copyPicture); 
    } 

    /** 
    * Constructor that takes a buffered image 
    * @param image the buffered image to use 
    */ 
    public Picture(BufferedImage image) 
    { 
    super(image); 
    } 

    ////////////////////// methods /////////////////////////////////////// 

    /** 
    * Method to return a string with information about this picture. 
    * @return a string with information about the picture such as fileName, 
    * height and width. 
    */ 
    public String toString() 
    { 
    String output = "Picture, filename " + getFileName() + 
     " height " + getHeight() 
     + " width " + getWidth(); 
    return output; 

    } 

    public void edgeDetection2(int edgeDist) 
    { 
     Pixel topPixel = null; 
     Pixel bottomPixel = null; 
     Pixel[][] pixels = this.getPixels2D(); 
     Color bottomColor = null; 
     for (int row = 0; row < pixels.length-1; row++) 
     { 
      for (int col = 0; 
       col < pixels[0].length; col++) 
      { 
      topPixel = pixels[row][col]; 
      bottomPixel = pixels[row+1][col]; 
      bottomColor = bottomPixel.getColor(); 
      if (topPixel.colorDistance(bottomColor) > 
       edgeDist) 
       topPixel.setColor(Color.BLACK); 
      else 
       topPixel.setColor(Color.WHITE); 
      } 
     } 
    } 

    /* Main method for testing - each class in Java can have a main 
    * method 
    */ 
    public static void main(String[] args) 
    { 
    Picture beach = new Picture("beach.jpg"); 
    beach.explore(); 
    } 

} // this } is the end of class Picture, put all new methods before this 
+0

Warum verwenden Sie 'Bild' anstelle von' BufferedImage'? –

+0

Dieser Code stammt aus einem Bildlaborprogramm für AP Computer Science. Ich habe noch nie von BufferedImage gehört. Ist ein Kantenerkennungsalgorithmus eingebaut? – sjgandhi2312

+2

Es ist schwierig zu wissen, wie man das Bild speichert, wenn wir nicht den Quellcode für Picture haben - eine Chance, den Bildcode hochzuladen? Außerdem ist BufferedImage die Haupt-Java-Bildklasse, die (am häufigsten) verwendet wird - https://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html –

Antwort

1

die „Bild“ Klasse ist etwas, das Ihre Klasse, die Ihnen zur Verfügung gestellt, es ist nicht ein in der Klasse gebaut. das sagte ich nahm eine klasse in java an meiner college und sie gaben mir auch eine klasse betitelte bild. versuchen Sie das

Picture myPicture = new Picture("input-file-name.jpg"); 
myPicture.write("name-for-new-file.jpg"); 
+0

Wo wird das Kantenbild gespeichert? – sjgandhi2312

+0

wird das Bild in seinem aktuellen Zustand gespeichert. Wenn deine Kantenerkennungsmethode das Schwan-Bild ändert, was ich glaube, wenn du beurteilst, was du gepostet hast, dann ja. – joey101937

Verwandte Themen