Add Java Image process
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.image.resize.core;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class Graphics2DExample {
|
||||
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D graphics2D = resizedImage.createGraphics();
|
||||
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
|
||||
graphics2D.dispose();
|
||||
return resizedImage;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-graphics2d.jpg"));
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.image.resize.core;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class ImageScaledInstanceExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
|
||||
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
bufferedImage.getGraphics()
|
||||
.drawImage(resultingImage, 0, 0, null);
|
||||
return bufferedImage;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-scaledinstance.jpg"));
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.image.resize.imgscalr;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.imgscalr.Scalr;
|
||||
|
||||
public class ImgscalrExample {
|
||||
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) {
|
||||
return Scalr.resize(originalImage, targetWidth);
|
||||
}
|
||||
|
||||
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-imgscalr.jpg"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.image.resize.marvin;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.marvinproject.image.transform.scale.Scale;
|
||||
|
||||
import marvin.image.MarvinImage;
|
||||
|
||||
public class MarvinExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
MarvinImage image = new MarvinImage(originalImage);
|
||||
Scale scale = new Scale();
|
||||
scale.load();
|
||||
scale.setAttribute("newWidth", targetWidth);
|
||||
scale.setAttribute("newHeight", targetHeight);
|
||||
scale.process(image.clone(), image, null, null, false);
|
||||
return image.getBufferedImageNoAlpha();
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws IOException {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-marvin.jpg"));
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.image.resize.thumbnailator;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
|
||||
public class ThumbnailatorExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
Thumbnails.of(originalImage)
|
||||
.size(targetWidth, targetHeight)
|
||||
.outputFormat("JPEG")
|
||||
.outputQuality(0.90)
|
||||
.toOutputStream(outputStream);
|
||||
byte[] data = outputStream.toByteArray();
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
|
||||
return ImageIO.read(inputStream);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-thumbnailator.jpg"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.imagefromwebcam;
|
||||
|
||||
import marvin.gui.MarvinImagePanel;
|
||||
import marvin.image.MarvinImage;
|
||||
import marvin.io.MarvinImageIO;
|
||||
import marvin.video.MarvinJavaCVAdapter;
|
||||
import marvin.video.MarvinVideoInterface;
|
||||
import marvin.video.MarvinVideoInterfaceException;
|
||||
|
||||
public class MarvinExample {
|
||||
|
||||
public static void main(String[] args) throws MarvinVideoInterfaceException {
|
||||
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
|
||||
videoAdapter.connect(0);
|
||||
MarvinImage image = videoAdapter.getFrame();
|
||||
MarvinImageIO.saveImage(image, "selfie.jpg");
|
||||
}
|
||||
|
||||
public void captureWithPanel() throws MarvinVideoInterfaceException {
|
||||
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
|
||||
videoAdapter.connect(0);
|
||||
MarvinImage image = videoAdapter.getFrame();
|
||||
|
||||
MarvinImagePanel imagePanel = new MarvinImagePanel();
|
||||
imagePanel.setImage(image);
|
||||
|
||||
imagePanel.setSize(800,600);
|
||||
imagePanel.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.imagefromwebcam;
|
||||
|
||||
import org.bytedeco.javacv.*;
|
||||
import org.bytedeco.opencv.opencv_core.IplImage;
|
||||
import java.awt.event.WindowEvent;
|
||||
import javax.swing.JFrame;
|
||||
import static org.bytedeco.opencv.helper.opencv_imgcodecs.cvSaveImage;
|
||||
|
||||
public class OpenCVExample {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
CanvasFrame canvas = new CanvasFrame("Web Cam");
|
||||
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
FrameGrabber grabber = new OpenCVFrameGrabber(0);
|
||||
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
|
||||
|
||||
grabber.start();
|
||||
Frame frame = grabber.grab();
|
||||
|
||||
IplImage img = converter.convert(frame);
|
||||
cvSaveImage("selfie.jpg", img);
|
||||
|
||||
canvas.showImage(frame);
|
||||
|
||||
Thread.sleep(2000);
|
||||
|
||||
canvas.dispatchEvent(new WindowEvent(canvas, WindowEvent.WINDOW_CLOSING));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.imagefromwebcam;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import com.github.sarxos.webcam.Webcam;
|
||||
import com.github.sarxos.webcam.WebcamPanel;
|
||||
import com.github.sarxos.webcam.WebcamResolution;
|
||||
import com.github.sarxos.webcam.util.ImageUtils;
|
||||
|
||||
public class WebcamCaptureExample {
|
||||
|
||||
public static void main(String[] args) throws IOException, Exception {
|
||||
Webcam webcam = Webcam.getDefault();
|
||||
webcam.open();
|
||||
|
||||
BufferedImage image = webcam.getImage();
|
||||
|
||||
ImageIO.write(image, ImageUtils.FORMAT_JPG, new File("selfie.jpg"));
|
||||
}
|
||||
|
||||
public void captureWithPanel() {
|
||||
Webcam webcam = Webcam.getDefault();
|
||||
webcam.setViewSize(WebcamResolution.VGA.getSize());
|
||||
|
||||
WebcamPanel panel = new WebcamPanel(webcam);
|
||||
panel.setImageSizeDisplayed(true);
|
||||
|
||||
JFrame window = new JFrame("Webcam");
|
||||
window.add(panel);
|
||||
window.setResizable(true);
|
||||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
window.pack();
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package com.baeldung.imageprocessing.addingtext;
|
||||
|
||||
import ij.IJ;
|
||||
import ij.ImagePlus;
|
||||
import ij.process.ImageProcessor;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.font.GlyphVector;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.AttributedString;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class AddText {
|
||||
public static void main(String[] args) throws IOException {
|
||||
String imagePath = AddText.class.getClassLoader().getResource("lena.jpg").getPath();
|
||||
|
||||
ImagePlus resultPlus = signImageImageProcessor("www.baeldung.com", imagePath);
|
||||
resultPlus.show();
|
||||
|
||||
ImagePlus resultGraphics = new ImagePlus("", signImageGraphics("www.baeldung.com", imagePath));
|
||||
resultGraphics.show();
|
||||
|
||||
ImagePlus resultGraphicsWithIterator = new ImagePlus("", signImageGraphicsWithIterator("www.baeldung.com", imagePath));
|
||||
resultGraphicsWithIterator.show();
|
||||
|
||||
ImagePlus resultGraphicsCentered = new ImagePlus("", signImageCenter("www.baeldung.com", imagePath));
|
||||
resultGraphicsCentered.show();
|
||||
|
||||
ImagePlus resultGraphicsBottomRight = new ImagePlus("", signImageBottomRight("www.baeldung.com", imagePath));
|
||||
resultGraphicsBottomRight.show();
|
||||
|
||||
ImagePlus resultGraphicsTopLeft = new ImagePlus("", signImageTopLeft("www.baeldung.com", imagePath));
|
||||
resultGraphicsTopLeft.show();
|
||||
|
||||
ImagePlus resultGraphicsAdaptBasedOnImage = new ImagePlus("", signImageAdaptBasedOnImage("www.baeldung.com", imagePath));
|
||||
resultGraphicsAdaptBasedOnImage.show();
|
||||
}
|
||||
|
||||
private static ImagePlus signImageImageProcessor(String text, String path) {
|
||||
ImagePlus image = IJ.openImage(path);
|
||||
Font font = new Font("Arial", Font.BOLD, 18);
|
||||
|
||||
ImageProcessor ip = image.getProcessor();
|
||||
ip.setColor(Color.GREEN);
|
||||
ip.setFont(font);
|
||||
ip.drawString(text, 0, 20);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private static BufferedImage signImageGraphics(String text, String path) throws IOException {
|
||||
BufferedImage image = ImageIO.read(new File(path));
|
||||
Font font = new Font("Arial", Font.BOLD, 18);
|
||||
|
||||
Graphics g = image.getGraphics();
|
||||
g.setFont(font);
|
||||
g.setColor(Color.GREEN);
|
||||
g.drawString(text, 0, 20);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
private static BufferedImage signImageGraphicsWithIterator(String text, String path) throws IOException {
|
||||
BufferedImage image = ImageIO.read(new File(path));
|
||||
Font font = new Font("Arial", Font.BOLD, 18);
|
||||
|
||||
AttributedString attributedText = new AttributedString(text);
|
||||
attributedText.addAttribute(TextAttribute.FONT, font);
|
||||
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
|
||||
|
||||
Graphics g = image.getGraphics();
|
||||
g.drawString(attributedText.getIterator(), 0, 20);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a String centered in the middle of a Rectangle.
|
||||
*
|
||||
* @param g The Graphics instance.
|
||||
* @param text The String to draw.
|
||||
* @param rect The Rectangle to center the text in.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static BufferedImage signImageCenter(String text, String path) throws IOException {
|
||||
|
||||
BufferedImage image = ImageIO.read(new File(path));
|
||||
Font font = new Font("Arial", Font.BOLD, 18);
|
||||
|
||||
AttributedString attributedText = new AttributedString(text);
|
||||
attributedText.addAttribute(TextAttribute.FONT, font);
|
||||
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
|
||||
|
||||
Graphics g = image.getGraphics();
|
||||
|
||||
FontMetrics metrics = g.getFontMetrics(font);
|
||||
int positionX = (image.getWidth() - metrics.stringWidth(text)) / 2;
|
||||
int positionY = (image.getHeight() - metrics.getHeight()) / 2 + metrics.getAscent();
|
||||
|
||||
g.drawString(attributedText.getIterator(), positionX, positionY);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a String centered in the middle of a Rectangle.
|
||||
*
|
||||
* @param g The Graphics instance.
|
||||
* @param text The String to draw.
|
||||
* @param rect The Rectangle to center the text in.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static BufferedImage signImageBottomRight(String text, String path) throws IOException {
|
||||
|
||||
BufferedImage image = ImageIO.read(new File(path));
|
||||
|
||||
Font font = new Font("Arial", Font.BOLD, 18);
|
||||
|
||||
AttributedString attributedText = new AttributedString(text);
|
||||
attributedText.addAttribute(TextAttribute.FONT, font);
|
||||
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
|
||||
|
||||
Graphics g = image.getGraphics();
|
||||
|
||||
FontMetrics metrics = g.getFontMetrics(font);
|
||||
int positionX = (image.getWidth() - metrics.stringWidth(text));
|
||||
int positionY = (image.getHeight() - metrics.getHeight()) + metrics.getAscent();
|
||||
|
||||
g.drawString(attributedText.getIterator(), positionX, positionY);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a String centered in the middle of a Rectangle.
|
||||
*
|
||||
* @param g The Graphics instance.
|
||||
* @param text The String to draw.
|
||||
* @param rect The Rectangle to center the text in.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static BufferedImage signImageTopLeft(String text, String path) throws IOException {
|
||||
|
||||
BufferedImage image = ImageIO.read(new File(path));
|
||||
|
||||
Font font = new Font("Arial", Font.BOLD, 18);
|
||||
|
||||
AttributedString attributedText = new AttributedString(text);
|
||||
attributedText.addAttribute(TextAttribute.FONT, font);
|
||||
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
|
||||
|
||||
Graphics g = image.getGraphics();
|
||||
|
||||
FontMetrics metrics = g.getFontMetrics(font);
|
||||
int positionX = 0;
|
||||
int positionY = metrics.getAscent();
|
||||
|
||||
g.drawString(attributedText.getIterator(), positionX, positionY);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a String centered in the middle of a Rectangle.
|
||||
*
|
||||
* @param g The Graphics instance.
|
||||
* @param text The String to draw.
|
||||
* @param rect The Rectangle to center the text in.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static BufferedImage signImageAdaptBasedOnImage(String text, String path) throws IOException {
|
||||
|
||||
BufferedImage image = ImageIO.read(new File(path));
|
||||
|
||||
Font font = createFontToFit(new Font("Arial", Font.BOLD, 80), text, image);
|
||||
|
||||
AttributedString attributedText = new AttributedString(text);
|
||||
attributedText.addAttribute(TextAttribute.FONT, font);
|
||||
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
|
||||
|
||||
Graphics g = image.getGraphics();
|
||||
|
||||
FontMetrics metrics = g.getFontMetrics(font);
|
||||
int positionX = (image.getWidth() - metrics.stringWidth(text));
|
||||
int positionY = (image.getHeight() - metrics.getHeight()) + metrics.getAscent();
|
||||
|
||||
g.drawString(attributedText.getIterator(), positionX, positionY);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
public static Font createFontToFit(Font baseFont, String text, BufferedImage image) throws IOException {
|
||||
Font newFont = baseFont;
|
||||
|
||||
FontMetrics ruler = image.getGraphics().getFontMetrics(baseFont);
|
||||
GlyphVector vector = baseFont.createGlyphVector(ruler.getFontRenderContext(), text);
|
||||
|
||||
Shape outline = vector.getOutline(0, 0);
|
||||
|
||||
double expectedWidth = outline.getBounds().getWidth();
|
||||
double expectedHeight = outline.getBounds().getHeight();
|
||||
|
||||
boolean textFits = image.getWidth() >= expectedWidth && image.getHeight() >= expectedHeight;
|
||||
|
||||
if (!textFits) {
|
||||
double widthBasedFontSize = (baseFont.getSize2D() * image.getWidth()) / expectedWidth;
|
||||
double heightBasedFontSize = (baseFont.getSize2D() * image.getHeight()) / expectedHeight;
|
||||
|
||||
double newFontSize = widthBasedFontSize < heightBasedFontSize ? widthBasedFontSize : heightBasedFontSize;
|
||||
newFont = baseFont.deriveFont(baseFont.getStyle(), (float) newFontSize);
|
||||
}
|
||||
return newFont;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.imageprocessing.imagej;
|
||||
|
||||
import ij.IJ;
|
||||
import ij.ImagePlus;
|
||||
import ij.process.ImageProcessor;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class ImageJRectExample {
|
||||
public static void main(String[] args) {
|
||||
ImagePlus imp = IJ.openImage(ImageJRectExample.class.getClassLoader().getResource("lena.jpg").getPath());
|
||||
drawRect(imp);
|
||||
imp.show();
|
||||
}
|
||||
|
||||
private static void drawRect(ImagePlus imp) {
|
||||
ImageProcessor ip = imp.getProcessor();
|
||||
ip.setColor(Color.BLUE);
|
||||
ip.setLineWidth(4);
|
||||
ip.drawRect(10, 10, imp.getWidth() - 20, imp.getHeight() - 20);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.imageprocessing.opencv;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfByte;
|
||||
import org.opencv.core.MatOfRect;
|
||||
import org.opencv.core.Rect;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.objdetect.CascadeClassifier;
|
||||
import org.opencv.objdetect.Objdetect;
|
||||
import org.opencv.videoio.VideoCapture;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
public class CameraStream extends Application {
|
||||
private VideoCapture capture;
|
||||
|
||||
public void start(Stage stage) throws Exception {
|
||||
// OpenCV.loadShared();
|
||||
capture= new VideoCapture(0); // The number is the ID of the camera
|
||||
ImageView imageView = new ImageView();
|
||||
HBox hbox = new HBox(imageView);
|
||||
Scene scene = new Scene(hbox);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
new AnimationTimer(){
|
||||
@Override
|
||||
public void handle(long l) {
|
||||
imageView.setImage(getCapture());
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public Image getCapture() {
|
||||
Mat mat = new Mat();
|
||||
capture.read(mat);
|
||||
return mat2Img(mat);
|
||||
}
|
||||
|
||||
public Image getCaptureWithFaceDetection() {
|
||||
Mat mat = new Mat();
|
||||
capture.read(mat);
|
||||
Mat haarClassifiedImg = detectFace(mat);
|
||||
return mat2Img(haarClassifiedImg);
|
||||
}
|
||||
|
||||
public Image mat2Img(Mat mat) {
|
||||
MatOfByte bytes = new MatOfByte();
|
||||
Imgcodecs.imencode("img", mat, bytes);
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes.toArray());
|
||||
Image img = new Image(inputStream); return img;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Application.launch(args);
|
||||
}
|
||||
|
||||
public static Mat detectFace(Mat inputImage) {
|
||||
MatOfRect facesDetected = new MatOfRect();
|
||||
CascadeClassifier cascadeClassifier = new CascadeClassifier();
|
||||
int minFaceSize = Math.round(inputImage.rows() * 0.1f);
|
||||
cascadeClassifier.load("./src/main/resources/haarcascades/haarcascade_frontalface_alt.xml");
|
||||
cascadeClassifier.detectMultiScale(inputImage,
|
||||
facesDetected,
|
||||
1.1,
|
||||
3,
|
||||
Objdetect.CASCADE_SCALE_IMAGE,
|
||||
new Size(minFaceSize, minFaceSize),
|
||||
new Size()
|
||||
);
|
||||
Rect[] facesArray = facesDetected.toArray();
|
||||
for(Rect face : facesArray) {
|
||||
Imgproc.rectangle(inputImage, face.tl(), face.br(), new Scalar(0, 0, 255), 3 );
|
||||
}
|
||||
return inputImage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.imageprocessing.opencv;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfByte;
|
||||
import org.opencv.core.MatOfRect;
|
||||
import org.opencv.core.Rect;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.objdetect.CascadeClassifier;
|
||||
import org.opencv.objdetect.Objdetect;
|
||||
import javafx.scene.image.Image;
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
public class FaceDetection {
|
||||
|
||||
public static Mat loadImage(String imagePath) {
|
||||
Imgcodecs imageCodecs = new Imgcodecs();
|
||||
return imageCodecs.imread(imagePath);
|
||||
}
|
||||
|
||||
public static void saveImage(Mat imageMatrix, String targetPath) {
|
||||
Imgcodecs imgcodecs = new Imgcodecs();
|
||||
imgcodecs.imwrite(targetPath, imageMatrix);
|
||||
}
|
||||
|
||||
public static void detectFace(String sourceImagePath, String targetImagePath) {
|
||||
Mat loadedImage = loadImage(sourceImagePath);
|
||||
MatOfRect facesDetected = new MatOfRect();
|
||||
CascadeClassifier cascadeClassifier = new CascadeClassifier();
|
||||
int minFaceSize = Math.round(loadedImage.rows() * 0.1f);
|
||||
cascadeClassifier.load("./src/main/resources/haarcascades/haarcascade_frontalface_alt.xml");
|
||||
cascadeClassifier.detectMultiScale(loadedImage,
|
||||
facesDetected,
|
||||
1.1,
|
||||
3,
|
||||
Objdetect.CASCADE_SCALE_IMAGE,
|
||||
new Size(minFaceSize, minFaceSize),
|
||||
new Size()
|
||||
);
|
||||
Rect[] facesArray = facesDetected.toArray();
|
||||
for(Rect face : facesArray) {
|
||||
Imgproc.rectangle(loadedImage, face.tl(), face.br(), new Scalar(0, 0, 255), 3 );
|
||||
}
|
||||
saveImage(loadedImage, targetImagePath);
|
||||
}
|
||||
|
||||
public Image mat2Img(Mat mat) {
|
||||
MatOfByte bytes = new MatOfByte();
|
||||
Imgcodecs.imencode("img", mat, bytes);
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes.toArray());
|
||||
Image img = new Image(inputStream); return img;
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.imageprocessing.openimaj;
|
||||
|
||||
import org.openimaj.image.DisplayUtilities;
|
||||
import org.openimaj.image.ImageUtilities;
|
||||
import org.openimaj.image.MBFImage;
|
||||
import org.openimaj.math.geometry.point.Point2d;
|
||||
import org.openimaj.math.geometry.point.Point2dImpl;
|
||||
import org.openimaj.math.geometry.shape.Polygon;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class OpenIMAJRectExample {
|
||||
public static void main(String[] args) throws IOException {
|
||||
MBFImage image = ImageUtilities.readMBF(OpenIMAJRectExample.class.getClassLoader().getResource("lena.jpg"));
|
||||
drawRectangle(image);
|
||||
DisplayUtilities.display(image);
|
||||
}
|
||||
|
||||
private static void drawRectangle(MBFImage image) {
|
||||
Point2d tl = new Point2dImpl(10, 10);
|
||||
Point2d bl = new Point2dImpl(10, image.getHeight() - 10);
|
||||
Point2d br = new Point2dImpl(image.getWidth() - 10, image.getHeight() - 10);
|
||||
Point2d tr = new Point2dImpl(image.getWidth() - 10, 10);
|
||||
Polygon polygon = new Polygon(Arrays.asList(tl, bl, br, tr));
|
||||
image.drawPolygon(polygon, 4, new Float[] { 0f, 0f, 255.0f });
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.imageprocessing.swing;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SwingRectExample {
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedImage image = loadImage();
|
||||
drawRectangle(image);
|
||||
displayImage(image);
|
||||
}
|
||||
|
||||
private static BufferedImage loadImage() throws IOException {
|
||||
String imagePath = SwingRectExample.class.getClassLoader().getResource("lena.jpg").getPath();
|
||||
return ImageIO.read(new File(imagePath));
|
||||
}
|
||||
|
||||
private static void drawRectangle(BufferedImage image) {
|
||||
Graphics2D g = (Graphics2D) image.getGraphics();
|
||||
g.setStroke(new BasicStroke(3));
|
||||
g.setColor(Color.BLUE);
|
||||
g.drawRect(10, 10, image.getWidth() - 20, image.getHeight() - 20);
|
||||
}
|
||||
|
||||
private static void displayImage(BufferedImage image) {
|
||||
JLabel picLabel = new JLabel(new ImageIcon(image));
|
||||
|
||||
JPanel jPanel = new JPanel();
|
||||
jPanel.add(picLabel);
|
||||
|
||||
JFrame f = new JFrame();
|
||||
f.setSize(new Dimension(image.getWidth(), image.getHeight()));
|
||||
f.add(jPanel);
|
||||
f.setVisible(true);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.imageprocessing.twelvemonkeys;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class TwelveMonkeysExample {
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedImage image = loadImage();
|
||||
drawRectangle(image);
|
||||
displayImage(image);
|
||||
}
|
||||
|
||||
private static BufferedImage loadImage() throws IOException {
|
||||
String imagePath = TwelveMonkeysExample.class.getClassLoader().getResource("Penguin.ico").getPath();
|
||||
return ImageIO.read(new File(imagePath));
|
||||
}
|
||||
|
||||
private static void drawRectangle(BufferedImage image) {
|
||||
Graphics2D g = (Graphics2D) image.getGraphics();
|
||||
g.setStroke(new BasicStroke(3));
|
||||
g.setColor(Color.BLUE);
|
||||
g.drawRect(10, 10, image.getWidth() - 20, image.getHeight() - 20);
|
||||
}
|
||||
|
||||
private static void displayImage(BufferedImage image) {
|
||||
JLabel picLabel = new JLabel(new ImageIcon(image));
|
||||
|
||||
JPanel jPanel = new JPanel();
|
||||
jPanel.add(picLabel);
|
||||
|
||||
JFrame f = new JFrame();
|
||||
f.setSize(new Dimension(200, 200));
|
||||
f.add(jPanel);
|
||||
f.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.tesseract;
|
||||
|
||||
import java.awt.Rectangle;
|
||||
import java.io.File;
|
||||
|
||||
import net.sourceforge.tess4j.Tesseract;
|
||||
import net.sourceforge.tess4j.TesseractException;
|
||||
|
||||
public class Tess4JExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String result = null;
|
||||
try {
|
||||
File image = new File("src/main/resources/images/baeldung.png");
|
||||
Tesseract tesseract = new Tesseract();
|
||||
tesseract.setLanguage("spa");
|
||||
tesseract.setPageSegMode(1);
|
||||
tesseract.setOcrEngineMode(1);
|
||||
tesseract.setHocr(true);
|
||||
tesseract.setDatapath("src/main/resources/tessdata");
|
||||
result = tesseract.doOCR(image, new Rectangle(1200, 200));
|
||||
} catch (TesseractException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.tesseract;
|
||||
|
||||
import org.bytedeco.javacpp.BytePointer;
|
||||
import org.bytedeco.leptonica.PIX;
|
||||
import org.bytedeco.tesseract.TessBaseAPI;
|
||||
|
||||
public class TesseractPlatformExample {
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
TessBaseAPI tessApi = new TessBaseAPI();
|
||||
tessApi.Init("src/main/resources/tessdata", "eng", 3);
|
||||
tessApi.SetPageSegMode(1);
|
||||
PIX image = org.bytedeco.leptonica.global.lept.pixRead("src/main/resources/images/baeldung.png");
|
||||
tessApi.SetImage(image);
|
||||
|
||||
BytePointer outText = tessApi.GetUTF8Text();
|
||||
System.out.println(outText.getString());
|
||||
tessApi.End();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user