Search in sources :

Example 1 with Java2DFrameConverter

use of org.bytedeco.javacv.Java2DFrameConverter in project instagram4j by brunocvcunha.

the class InstagramUploadVideoRequest method configureThumbnail.

/**
 * Configures the thumbnails for the given uploadId
 * @param uploadId The session id
 * @return Result
 * @throws Exception
 * @throws IOException
 * @throws ClientProtocolException
 */
protected StatusResult configureThumbnail(String uploadId) throws Exception, IOException, ClientProtocolException {
    try (FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoFile)) {
        frameGrabber.start();
        Java2DFrameConverter converter = new Java2DFrameConverter();
        int width = frameGrabber.getImageWidth();
        int height = frameGrabber.getImageHeight();
        long length = frameGrabber.getLengthInTime();
        BufferedImage bufferedImage;
        if (thumbnailFile == null) {
            bufferedImage = MyImageUtils.deepCopy(converter.convert(frameGrabber.grabImage()));
            thumbnailFile = File.createTempFile("insta", ".jpg");
            log.info("Generated thumbnail: " + thumbnailFile.getAbsolutePath());
            ImageIO.write(bufferedImage, "JPG", thumbnailFile);
        } else {
            bufferedImage = ImageIO.read(thumbnailFile);
        }
        holdOn();
        StatusResult thumbnailResult = api.sendRequest(new InstagramUploadPhotoRequest(thumbnailFile, caption, uploadId));
        log.info("Thumbnail result: " + thumbnailResult);
        StatusResult configureResult = api.sendRequest(InstagramConfigureVideoRequest.builder().uploadId(uploadId).caption(caption).duration(length).width(width).height(height).build());
        log.info("Video configure result: " + configureResult);
        return configureResult;
    }
}
Also used : FFmpegFrameGrabber(org.bytedeco.javacv.FFmpegFrameGrabber) StatusResult(org.brunocvcunha.instagram4j.requests.payload.StatusResult) BufferedImage(java.awt.image.BufferedImage) Java2DFrameConverter(org.bytedeco.javacv.Java2DFrameConverter)

Example 2 with Java2DFrameConverter

use of org.bytedeco.javacv.Java2DFrameConverter in project javacv by bytedeco.

the class ColoredObjectTrack method Equalize.

public IplImage Equalize(BufferedImage bufferedimg) {
    Java2DFrameConverter converter1 = new Java2DFrameConverter();
    OpenCVFrameConverter.ToIplImage converter2 = new OpenCVFrameConverter.ToIplImage();
    IplImage iploriginal = converter2.convert(converter1.convert(bufferedimg));
    IplImage srcimg = IplImage.create(iploriginal.width(), iploriginal.height(), IPL_DEPTH_8U, 1);
    IplImage destimg = IplImage.create(iploriginal.width(), iploriginal.height(), IPL_DEPTH_8U, 1);
    cvCvtColor(iploriginal, srcimg, CV_BGR2GRAY);
    cvEqualizeHist(srcimg, destimg);
    return destimg;
}
Also used : OpenCVFrameConverter(org.bytedeco.javacv.OpenCVFrameConverter) Java2DFrameConverter(org.bytedeco.javacv.Java2DFrameConverter)

Example 3 with Java2DFrameConverter

use of org.bytedeco.javacv.Java2DFrameConverter in project javacv by bytedeco.

the class JavaFxPlayVideoAndAudio method start.

@Override
public void start(Stage primaryStage) throws Exception {
    StackPane root = new StackPane();
    ImageView imageView = new ImageView();
    root.getChildren().add(imageView);
    imageView.fitWidthProperty().bind(primaryStage.widthProperty());
    imageView.fitHeightProperty().bind(primaryStage.heightProperty());
    Scene scene = new Scene(root, 640, 480);
    primaryStage.setTitle("Video + audio");
    primaryStage.setScene(scene);
    primaryStage.show();
    playThread = new Thread(() -> {
        try {
            String videoFilename = getParameters().getRaw().get(0);
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFilename);
            grabber.start();
            primaryStage.setWidth(grabber.getImageWidth());
            primaryStage.setHeight(grabber.getImageHeight());
            AudioFormat audioFormat = new AudioFormat(grabber.getSampleRate(), 16, grabber.getAudioChannels(), true, true);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
            SourceDataLine soundLine = (SourceDataLine) AudioSystem.getLine(info);
            soundLine.open(audioFormat);
            soundLine.start();
            Java2DFrameConverter converter = new Java2DFrameConverter();
            ExecutorService executor = Executors.newSingleThreadExecutor();
            while (!Thread.interrupted()) {
                Frame frame = grabber.grab();
                if (frame == null) {
                    break;
                }
                if (frame.image != null) {
                    Image image = SwingFXUtils.toFXImage(converter.convert(frame), null);
                    Platform.runLater(() -> {
                        imageView.setImage(image);
                    });
                } else if (frame.samples != null) {
                    ShortBuffer channelSamplesShortBuffer = (ShortBuffer) frame.samples[0];
                    channelSamplesShortBuffer.rewind();
                    ByteBuffer outBuffer = ByteBuffer.allocate(channelSamplesShortBuffer.capacity() * 2);
                    for (int i = 0; i < channelSamplesShortBuffer.capacity(); i++) {
                        short val = channelSamplesShortBuffer.get(i);
                        outBuffer.putShort(val);
                    }
                    /**
                     * We need this because soundLine.write ignores
                     * interruptions during writing.
                     */
                    try {
                        executor.submit(() -> {
                            soundLine.write(outBuffer.array(), 0, outBuffer.capacity());
                            outBuffer.clear();
                        }).get();
                    } catch (InterruptedException interruptedException) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
            executor.shutdownNow();
            executor.awaitTermination(10, TimeUnit.SECONDS);
            soundLine.stop();
            grabber.stop();
            grabber.release();
            Platform.exit();
        } catch (Exception exception) {
            LOG.log(Level.SEVERE, null, exception);
            System.exit(1);
        }
    });
    playThread.start();
}
Also used : Frame(org.bytedeco.javacv.Frame) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) Scene(javafx.scene.Scene) Image(javafx.scene.image.Image) ByteBuffer(java.nio.ByteBuffer) FFmpegFrameGrabber(org.bytedeco.javacv.FFmpegFrameGrabber) ExecutorService(java.util.concurrent.ExecutorService) SourceDataLine(javax.sound.sampled.SourceDataLine) ImageView(javafx.scene.image.ImageView) AudioFormat(javax.sound.sampled.AudioFormat) ShortBuffer(java.nio.ShortBuffer) StackPane(javafx.scene.layout.StackPane) Java2DFrameConverter(org.bytedeco.javacv.Java2DFrameConverter)

Example 4 with Java2DFrameConverter

use of org.bytedeco.javacv.Java2DFrameConverter in project javacv by bytedeco.

the class OpenCVFeatures2dSerialization method main.

public static void main(String[] args) throws IOException {
    String imageFile = (args.length > 0) ? args[0] : "Blob3.jpg";
    BufferedImage bufferedImage = ImageIO.read(new File(imageFile));
    try (Mat matrix = new OpenCVFrameConverter.ToMat().convert(new Java2DFrameConverter().convert(bufferedImage))) {
        String fileName = "serialized.xml";
        serializeFile(matrix, fileName);
        deserializeFile(fileName);
        String serialized = serializeMemory(matrix);
        System.out.println(serialized);
        deserializeMemory(serialized);
    }
}
Also used : OpenCVFrameConverter(org.bytedeco.javacv.OpenCVFrameConverter) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Java2DFrameConverter(org.bytedeco.javacv.Java2DFrameConverter)

Example 5 with Java2DFrameConverter

use of org.bytedeco.javacv.Java2DFrameConverter in project javacv by bytedeco.

the class PrincipalComponentAnalysis method execute.

private void execute(String[] args) throws Exception {
    // If no params provided, compute the default image
    BufferedImage bufferedImage = args.length >= 1 ? ImageIO.read(new File(args[0])) : ImageIO.read(this.getClass().getResourceAsStream("shapes2.jpg"));
    System.out.println("Image type: " + bufferedImage.getType());
    // Convert BufferedImage to Mat and create AutoCloseable objects
    try (Mat matrix = new OpenCVFrameConverter.ToMat().convert(new Java2DFrameConverter().convert(bufferedImage));
        Mat mask = new Mat();
        Mat gray = new Mat();
        Mat denoised = new Mat();
        Mat bin = new Mat();
        Mat hierarchy = new Mat();
        MatVector contours = new MatVector()) {
        printMat(matrix);
        cvtColor(matrix, gray, COLOR_BGR2GRAY);
        // Normalize
        GaussianBlur(gray, denoised, new Size(5, 5), 0);
        threshold(denoised, mask, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
        normalize(gray, gray, 0, 255, NORM_MINMAX, -1, mask);
        // Convert image to binary
        threshold(gray, bin, 150, 255, THRESH_BINARY);
        // Find contours
        findContours(bin, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
        long contourCount = contours.size();
        System.out.println("Countour count " + contourCount);
        for (int i = 0; i < contourCount; ++i) {
            // Calculate the area of each contour
            Mat contour = contours.get(i);
            double area = contourArea(contour);
            // Ignore contours that are too small or too large
            if (area > 128 && area < 8192) {
                principalComponentAnalysis(contour, i, matrix);
            }
        }
        CanvasFrame canvas = new CanvasFrame("PrincipalComponentAnalysis", 1);
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        canvas.setCanvasSize(320, 240);
        OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
        canvas.showImage(converter.convert(matrix));
    }
}
Also used : BufferedImage(java.awt.image.BufferedImage) OpenCVFrameConverter(org.bytedeco.javacv.OpenCVFrameConverter) File(java.io.File) Java2DFrameConverter(org.bytedeco.javacv.Java2DFrameConverter) CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Aggregations

Java2DFrameConverter (org.bytedeco.javacv.Java2DFrameConverter)7 BufferedImage (java.awt.image.BufferedImage)5 OpenCVFrameConverter (org.bytedeco.javacv.OpenCVFrameConverter)5 File (java.io.File)4 FFmpegFrameGrabber (org.bytedeco.javacv.FFmpegFrameGrabber)2 ByteBuffer (java.nio.ByteBuffer)1 ShortBuffer (java.nio.ShortBuffer)1 ExecutorService (java.util.concurrent.ExecutorService)1 Scene (javafx.scene.Scene)1 Image (javafx.scene.image.Image)1 ImageView (javafx.scene.image.ImageView)1 StackPane (javafx.scene.layout.StackPane)1 AudioFormat (javax.sound.sampled.AudioFormat)1 DataLine (javax.sound.sampled.DataLine)1 SourceDataLine (javax.sound.sampled.SourceDataLine)1 StatusResult (org.brunocvcunha.instagram4j.requests.payload.StatusResult)1 CanvasFrame (org.bytedeco.javacv.CanvasFrame)1 Frame (org.bytedeco.javacv.Frame)1