📄 photo.java
字号:
/*
------------------------------------------------------------------------
JavaANPR - Automatic Number Plate Recognition System for Java
------------------------------------------------------------------------
This file is a part of the JavaANPR, licensed under the terms of the
Educational Community License
Copyright (c) 2006-2007 Ondrej Martinsky. All rights reserved
This Original Work, including software, source code, documents, or
other related items, is being provided by the copyright holder(s)
subject to the terms of the Educational Community License. By
obtaining, using and/or copying this Original Work, you agree that you
have read, understand, and will comply with the following terms and
conditions of the Educational Community License:
Permission to use, copy, modify, merge, publish, distribute, and
sublicense this Original Work and its documentation, with or without
modification, for any purpose, and without fee or royalty to the
copyright holder(s) is hereby granted, provided that you include the
following on ALL copies of the Original Work or portions thereof,
including modifications or derivatives, that you make:
# The full text of the Educational Community License in a location
viewable to users of the redistributed or derivative work.
# Any pre-existing intellectual property disclaimers, notices, or terms
and conditions.
# Notice of any changes or modifications to the Original Work,
including the date the changes were made.
# Any modifications of the Original Work must be distributed in such a
manner as to avoid any confusion with the Original Work of the
copyright holders.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The name and trademarks of copyright holder(s) may NOT be used in
advertising or publicity pertaining to the Original or Derivative Works
without specific, written prior permission. Title to copyright in the
Original Work and any associated documentation will at all times remain
with the copyright holders.
If you want to alter upon this work, you MUST attribute it in
a) all source files
b) on every place, where is the copyright of derivated work
exactly by the following label :
---- label begin ----
This work is a derivate of the JavaANPR. JavaANPR is a intellectual
property of Ondrej Martinsky. Please visit http://javaanpr.sourceforge.net
for more info about JavaANPR.
---- label end ----
------------------------------------------------------------------------
http://javaanpr.sourceforge.net
------------------------------------------------------------------------
*/
package javaanpr.imageanalysis;
//import com.sun.java_cup.internal.Main;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
//import java.awt.image.IndexColorModel;
import java.awt.image.Kernel;
import java.awt.image.LookupOp;
import java.awt.image.ShortLookupTable;
//import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
//import java.util.Vector;
import javax.imageio.ImageIO;
//import javax.print.attribute.standard.OutputDeviceAssigned;
import javaanpr.intelligence.Intelligence;
//import javaanpr.recognizer.CharacterRecognizer;
public class Photo {
public BufferedImage image;
public Photo() {
this.image=null;
}
public Photo(BufferedImage bi) {
this.image=bi;
}
public Photo(String filepath) throws IOException {
this.loadImage(filepath);
}
public Photo clone() {
return new Photo(this.duplicateBufferedImage(this.image));
}
public int getWidth() {
return this.image.getWidth();
}
public int getHeight() {
return this.image.getHeight();
}
public int getSquare() {
return this.getWidth() * this.getHeight();
}
public BufferedImage getBi() {
return this.image;
}
public BufferedImage getBiWithAxes() {
BufferedImage axis = new BufferedImage(this.image.getWidth()+40,
this.image.getHeight()+40, BufferedImage.TYPE_INT_RGB);
Graphics2D graphicAxis = axis.createGraphics();
graphicAxis.setColor(Color.LIGHT_GRAY);
Rectangle backRect = new Rectangle(0,0,this.image.getWidth()+40,this.image.getHeight()+40);
graphicAxis.fill(backRect);
graphicAxis.draw(backRect);
graphicAxis.drawImage(this.image,35,5,null);
graphicAxis.setColor(Color.BLACK);
graphicAxis.drawRect(35,5,this.image.getWidth(), this.image.getHeight());
for (int ax = 0; ax < this.image.getWidth(); ax += 50) {
graphicAxis.drawString(new Integer(ax).toString() , ax + 35, axis.getHeight()-10);
graphicAxis.drawLine(ax+35, this.image.getHeight()+5 ,ax+35, this.image.getHeight()+15);
}
for (int ay = 0; ay < this.image.getHeight(); ay += 50) {
graphicAxis.drawString(new Integer(ay).toString(), 3 ,ay + 15);
graphicAxis.drawLine(25,ay+5,35,ay+5);
}
graphicAxis.dispose();
return axis;
}
public void setBrightness(int x, int y, float value) {
image.setRGB(x,y, new Color(value,value,value).getRGB() );
}
static public void setBrightness(BufferedImage image, int x, int y, float value) {
image.setRGB(x,y, new Color(value,value,value).getRGB() );
}
static public float getBrightness(BufferedImage image, int x, int y) {
int r = image.getRaster().getSample(x,y,0);
int g = image.getRaster().getSample(x,y,1);
int b = image.getRaster().getSample(x,y,2);
float[] hsb = Color.RGBtoHSB(r,g,b,null);
return hsb[2];
}
static public float getSaturation(BufferedImage image, int x, int y) {
int r = image.getRaster().getSample(x,y,0);
int g = image.getRaster().getSample(x,y,1);
int b = image.getRaster().getSample(x,y,2);
float[] hsb = Color.RGBtoHSB(r,g,b,null);
return hsb[1];
}
static public float getHue(BufferedImage image, int x, int y) {
int r = image.getRaster().getSample(x,y,0);
int g = image.getRaster().getSample(x,y,1);
int b = image.getRaster().getSample(x,y,2);
float[] hsb = Color.RGBtoHSB(r,g,b,null);
return hsb[0];
}
public float getBrightness(int x, int y) {
return getBrightness(image,x,y);
}
public float getSaturation(int x, int y) {
return getSaturation(image,x,y);
}
public float getHue(int x, int y) {
return getHue(image,x,y);
}
public void loadImage(String filepath) throws IOException {
try {
File source = new File(filepath);
BufferedImage image = ImageIO.read(source);
BufferedImage outimage = new BufferedImage(image.getWidth(),image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = outimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
this.image = outimage;
} catch (IOException ex) {
throw new IOException("{Error in image loader} Couldn't read input file "+filepath);
}
}
public void saveImage(String filepath) throws IOException {
String type = new String(filepath.substring(filepath.lastIndexOf('.')+1,filepath.length()).toUpperCase());
if (!type.equals("BMP") &&
!type.equals("JPG") &&
!type.equals("JPEG") &&
!type.equals("PNG")
) throw new IOException("Unsupported file format");
File destination = new File(filepath);
ImageIO.write(this.image, type, destination);
}
public void normalizeBrightness(float coef) {
Statistics stats = new Statistics(this);
for (int x=0; x<this.getWidth(); x++) {
for (int y=0; y<this.getHeight(); y++) {
this.setBrightness(this.image,x,y,
stats.thresholdBrightness(this.getBrightness(this.image,x,y), coef)
);
}
}
}
// FILTERS
public void linearResize(int width, int height) {
this.image = linearResizeBi(this.image,width,height);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -