Skip to content

Instantly share code, notes, and snippets.

@guffyWave
Created June 19, 2015 06:20
Show Gist options
  • Save guffyWave/92d18c58d45705d3287a to your computer and use it in GitHub Desktop.
Save guffyWave/92d18c58d45705d3287a to your computer and use it in GitHub Desktop.
Working IplImage in JavaCV
package com.appxperts.opencvfirstapp;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_imgproc;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameConverter;
import java.io.File;
/**
* Created by PC on 6/18/2015.
*/
public class MainActivitySecond extends Activity {
ImageView image_view;
private static final String TAG = MainActivity.class.getSimpleName();
private static final String IMAGE_PATH = Environment.getExternalStorageDirectory() + File.separator + "RanjanJavaCv" + File.separator;
// private static final String VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
//private static final String VIDEO_URL = "http://r5---sn-cvh7zn7k.c.youtube.com/videoplayback?id=32541e2e84d4a191&sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=903309%2C906398%2C935031%2C916611%2C924616%2C924610%2C907231&sver=3&cp=U0hXR1ZNTl9FSkNON19OR1JKOmtlYmdzNVE4ZG5N&upn=VopUQRFCawg&ms=au&mt=1382596803&mv=u&ip=59.162.119.108&key=yt5&itag=18&expire=1382620900&ratebypass=yes&ipbits=8&source=youtube&signature=E77065B4FAC80972F255857BB16856C601F77734.BBD905340F324DFD55BCF39D31617EE9A7FB1B37";
private static final File m_file = new File(Environment.getExternalStorageDirectory() + File.separator, "VideoEffects" + File.separator + "splash_video.mp4");
final long MICROSECOND = 1000000;
private File m_imageDirectory = null;
private Button m_btnGrab = null;
//private int m_count = 1;
private volatile int m_frameCount = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image_view = (ImageView) findViewById(R.id.image_view);
m_btnGrab = (Button) findViewById(R.id.button);
m_imageDirectory = new File(IMAGE_PATH);
if (!m_imageDirectory.exists()) {
m_imageDirectory.mkdirs();
}
m_btnGrab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
kaamdg();
// new GrabberThread(1, 5).start();
//new GrabberThread(6, 10).start();
//new GrabberThread(11, 15).start();
}
});
}
public void kaamdg() {
;
FFmpegFrameGrabber ffg = new FFmpegFrameGrabber(m_file.getAbsolutePath());
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
try {
ffg.start();
int count = 0;
while (true) {
Log.d("JAVA_CV", "Counting . . . " + count);
Frame original_frame = ffg.grab();
if (original_frame == null) {
Log.d("JAVA_CV", "original_frame is NULL at" + count);
break;
} else {
Log.d("JAVA_CV", "original_frame is NOT_NULL at" + count);
opencv_core.IplImage frame1 = converter.convert(original_frame);
if (frame1 == null) {
Log.d("JAVA_CV", "frame1 is NULL at" + count);
} else {
if(count==4) {
int width = frame1.width();
int height = frame1.height();
int channel = frame1.nChannels();
opencv_core.IplImage frame2 = opencv_core.IplImage.create(width, height, opencv_core.IPL_DEPTH_8U, channel);
opencv_core.cvCopy(frame1,frame2);
opencv_core.IplImage frame3 = opencv_core.IplImage.create(width, height, opencv_core.IPL_DEPTH_8U, 1);
opencv_imgproc.cvCvtColor(frame2, frame3, opencv_imgproc.CV_BGRA2GRAY);
Bitmap m_bitmap = Bitmap.createBitmap(frame3.width(), frame3.height(), Bitmap.Config.ALPHA_8);
m_bitmap.copyPixelsFromBuffer(frame2.getByteBuffer());
image_view.setImageBitmap(m_bitmap);
Log.d("JAVA_CV", "frame1 is NOT_NULL at" + count + " Height " + frame1.height());
break;
}
}
//opencv_core.IplImage frame2 = opencv_core.IplImage.create(original_frame.imageWidth, original_frame.imageHeight, opencv_core.IPL_DEPTH_8U, 0);
// opencv_imgproc.cvCvtColor(frame1, frame2, opencv_imgproc.CV_RGB2GRAY);
// opencv_imgproc.
// if (count == 0) {
// Bitmap m_bitmap = Bitmap.createBitmap(frame2.width(), frame2.height(), Bitmap.Config.ARGB_8888);
// m_bitmap.copyPixelsFromBuffer(frame2.getByteBuffer());
// image_view.setImageBitmap(m_bitmap);
// break;
// }
}
count++;
}
} catch (FrameGrabber.Exception fe) {
fe.printStackTrace();
System.out.println(fe.getMessage());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
/* private void startProcess() {
FFmpegFrameGrabber ffg = new FFmpegFrameGrabber(m_file.getAbsolutePath());
try {
ffg.start();
System.out.println("Format " + ffg.getFormat());
// System.out.println("Audio BitRate " + ffg.getAudioBitrate());
System.out.println("Audio Channels " + ffg.getAudioChannels());
//System.out.println("Audio Codec " + ffg.getAudioCodec());
System.out.println("Frame Rate" + ffg.getFrameRate());
System.out.println("Gamma " + ffg.getGamma());
System.out.println("Image Height " + ffg.getImageHeight());
System.out.println("Image Width " + ffg.getImageWidth());
// System.out.println("Length in Frame " + ffg.getLengthInFrames());
System.out.println("Pixel Format " + ffg.getPixelFormat());
System.out.println("Sample Format " + ffg.getSampleFormat());
System.out.println("Sample Rate " + ffg.getSampleRate());
//System.out.println("Video Bitrate " + ffg.getVideoBitrate());
// System.out.println("Video Codec " + ffg.getVideoCodec());
System.out.println("Frame Number " + ffg.getFrameNumber());
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
int count = 0;
while (true) {
Frame original_frame = ffg.grab();
if (original_frame == null) {
Log.d("JAVA_CV", "original_frame is NULL at" + count);
break;
} else {
if (original_frame!=null) {
opencv_core.IplImage implIlame1 = converter.convert(original_frame);
int width = implIlame1.width();
int height = implIlame1.height();
opencv_core.IplImage frame2 = opencv_core.IplImage.create(width, height, opencv_core.IPL_DEPTH_8U, 4);
opencv_imgproc.cvCvtColor(implIlame1, frame2, opencv_imgproc.CV_BGR2RGBA);
Log.d("JAVA_CV", "original_frame is NOT_NULL at" + count);
}
// opencv_core.IplImage implIlame2 = opencv_core.IplImage.create(implIlame1.width(), implIlame1.height(), opencv_core.IPL_DEPTH_8U, 4);
// opencv_imgproc.cvCvtColor(implIlame1, implIlame2, opencv_imgproc.CV_BGR2RGBA);
// if (count == 0) {
// Bitmap m_bitmap = Bitmap.createBitmap(frame2.width(), frame2.height(), Bitmap.Config.ARGB_8888);
// m_bitmap.copyPixelsFromBuffer(frame2.getByteBuffer());
// image_view.setImageBitmap(m_bitmap);
// break;
// }
// frame1.release();
// frame2.release();
//
// ffg.release();
}
count++;
}
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
Log.e("JAVA_CV", "Exception " + e);
}
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment