show a picture in memory of AVD ..using OpenCV - java

I want to draw a line on a picture in memory of android device and show it.I put a picture in sdcard of AVD . I installed opencv libraries and they worked. But I don't know how to draw a line on a picture by OpenCV on android and for showing a picture I have problem. My codes doesn't have any error.But when I run app in AVD my App had been stopped by android ! My codes are:
public class MainActivity extends Activity {
**#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mat a=Highgui.imread("/mnt/sdcard/img5.jpg");
Bitmap bm = Bitmap.createBitmap(a.cols(), a.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(a, bm);
ImageView iw=new ImageView(this);
iw.setImageBitmap(bm);
setContentView(iw);
}

you can't use any opencv code in onCreate() .
since it is dependant on native libs, you've got to wait , until they were properly loaded (until mLoaderCallback returns).
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
if (status == LoaderCallbackInterface.SUCCESS ) {
//
// now we can call opencv code !
//
} else {
super.onManagerConnected(status);
}
}
onCameraViewStarted() might be the best place to put your code (with a camera setup).

Related

Gif is not working for the first time using glide

I am making an app that has a list of two items that have two gifs, one in each (the same gif). As you can see in the video, when you click on the gif, the gif is not shown in motion and it is shown as a solid image, but when you click the right or left arrow it is shown correctly in motion despite that the function to load the gif is called in both the OnCreate function and the btn_next.setOnClickListener() function and the btn_prev.setOnClickListener() function. So the same thing is done but onCreate() the gif is not shown in motion and on btn_next and btn_prev setOnClickListener() it is shown. Please, someone could tell me what could be happening here? Bcause actually the same code is executed but on the first clikc is not working
Video: https://youtube.com/shorts/S5fDsqu3CeI?feature=share
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
init();
glide();
}
private void init() {
img_1 = findViewById(R.id.img_image1);
btn_prev = findViewById(R.id.dialog_btn_prev);
btn_next = findViewById(R.id.dialog_btn_next);
btn_next.setOnClickListener(view -> {
glide();
});
btn_prev.setOnClickListener(view -> {
glide();
}
});
}
public void glide() {
Glide.with(getApplicationContext()).load(R.drawable.earth).into(img_1);
}
Change your code when you are initialising Glide with this.
Glide.with(this).asGif().load(R.raw. earth).into(img_1);
Try this!
Glide.with(this)
.asGif()
.load(R.raw._img)
.centerCrop()
.into(new ImageViewTarget<GifDrawable>(img) {
#Override
protected void setResource(#Nullable GifDrawable resource) {
img.setImageDrawable(resource);
}
});
Add dependencies build.gradle(App Level)
implementation 'com.github.bumptech.glide:glide:4.13.2'

Android switching between gl views causes app freeze

I'm still learning how to program in Android and I created an Activity with a start view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mMainView = (RajawaliSurfaceView) findViewById(R.id.my_view);
mMainView.setSurfaceRenderer(new Renderer());
mMainView.setZOrderOnTop(false);
// Also set up a surface view I'd like to switch on back and forth
mGLView = new GLSurfaceView(this);
mGLView.setEGLContextClientVersion(2);
mGLView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
mGLView.setRenderer(cameraRenderer_ = new GLRenderer(this));
mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
This works and I can even switch to the GL view seamlessly:
public void switchToGLView(View view) {
setContentView(mGLView);
}
But after I'm done with the GL thing, I have the GLRenderer call a restore function in my main activity
public void restoreRenderer() {
// Restoring the previous view (my_view) with the non-GL renderer
setContentView(mMainView); <<<<<< FREEZES HERE (blackscreen)
}
and unfortunately this never works: the screen remains black from the previous GL renderer and by debugging the setContentView line I discovered that:
I don't get ANY error or warning log in the Android Monitor
The execution NEVER continues. It gets stuck in there forever
Any tips or hints on how to solve this?
You could solve this problem using a Frame or Relative layout.
If I understand correctly, both mMainView and mGLView are fullscreen.
There are a few options:
You could change the visibility of the views:
public void switchViews() {
if(mMainView.getVisibility() == View.VISIBLE){
mMainView.setVisibility(View.GONE);
mGLView.setVisibility(View.VISIBLE);
} else{
mMainView.setVisibility(View.VISIBLE);
mGLView.setVisibility(View.GONE);
}
}
The other option is to change the Z order of the views:
public void switchViews(View view){
view.bringToFront();
view.invalidate();
}
Unless there are any drawbacks you encountered using the above methods.

Button to switch between front and back camera in my camera app in android studio and face detection

I'm developping a camera app on android studio using openCv 3.0.0.It's my first time doing this and I'm facing some problems. But I have 2 issues:
1) I want to add a button to switch between the front camera and the back camera. But I can't seem to find a way to switch.
Here is my onCreate method:
private Camera mCamera;
private CameraPreview mPreview;
private static int number = Camera.CameraInfo.CAMERA_FACING_FRONT;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.face_detect_surface_view);
// Create an instance of Camera
mCamera = getCameraInstance(number);// This funtion opens the camera
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, number , mCamera);
final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
// Add a listener to the Capture button
ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Image captured!", Toast.LENGTH_SHORT).show();
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);
// Add a listener to the Change button
ImageButton changeButton = (ImageButton) findViewById(R.id.button_change);
changeButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (number == Camera.CameraInfo.CAMERA_FACING_FRONT)
number = Camera.CameraInfo.CAMERA_FACING_BACK;
else
number = Camera.CameraInfo.CAMERA_FACING_FRONT;
//HERE SHOULD BE THE STEPS TO SWITCH
Toast.makeText(getApplicationContext(), "Camera changed!", Toast.LENGTH_SHORT).show();
// get an image from the camera
}
}
);
}
2)I want to use openCv for face detection on the image that's captures but I don't know whether it's possible. I couldn't find anything on the web. I've already tried the example faceDetect from openCv 3.0.0 and it worked when I was using the camera. That's what I wanted to do at first but after I changed my layout to contain a frame layout instead of org.opencv.android.JavaCameraView it doesn't work anymore. So if anyone has an idea why I'd be very grateful.
All *CameraView classes have disableView and enableView methods. You need to disable view, set mCameraIndex field of View object and enable view again. mCameraView is protected method, so the only solution is to implement view subclass and setters/getters. See tutorial-3 example for more details.
<org.opencv.android.NativeCameraView
android:id="#+id/tutorial1_activity_native_surface_view"
android:layout_width="350px"
android:layout_height="350px"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
opencv:camera_id="front" />

App crashes when using bitmap and mat with OpenCV

So I would like to start developing apps using OpenCV. I want try something simple, read an image, convert it to grayscale and then display it.
When I read it and convert it using Mat, the app works fine. When I read it and display it using Bitmaps it works fine as well. When I combine both, read the image using mat, convert it to bitmap it crashes. I don't have a logcat because I can't play the app straight away from the computer so I have to send the .apk every time ...
I downloaded LogcatBrowser and took a screenshot of what I obtained ...
MainAcvitity.Java code:
public class MainActivity extends Activity {
private Bitmap mBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
if (status == LoaderCallbackInterface.SUCCESS ) {
ImageView img = (ImageView)findViewById(R.id.image_view_camera);
Mat m = Highgui.imread("gph.jpg");
//Imgproc.cvtColor(m, m, Imgproc.COLOR_BGR2GRAY);
Bitmap resultBitmap = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, mBitmap);
img.setImageBitmap(mBitmap);
} else {
super.onManagerConnected(status);
}
}
};
#Override
public void onResume() {;
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
}
}

How not to seek to keyframes

I've tried to find a solution to seek frame by frame (not only to keyframes) in my android app.
approach: A simple VideoView of the android sdk:
Here I've got a onSeekCompleteListener of the base class MediaPlayer from the onPreparedListener of VideoView and pause the playback right after started it and moved to a preferred position.
Problem: Only seeks to keyframes!
approach: MediaMetaRetriever:
I have got something like this:
MediaMetadataRetriever retriever;
long Position;
ImageView video;
TextView lbPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (ImageView)findViewById(R.id.imVideo);
lbPosition = (TextView)findViewById(R.id.lbPosition);
bnTake = (Button)findViewById(R.id.bntake);
retriever.setDataSource(this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.vid));
video.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View arg0) {
Position += 100000;
Bitmap frame = retriever.getFrameAtTime(Position, MediaMetadataRetriever.OPTION_CLOSEST);
video.setImageBitmap(frame);
lbPosition.setText(String.valueOf(Position));
}
});
bnTake.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent, 123123);
}
});
}
When I use OPTION_CLOSESTS in retriever.getFrameAtTime it gives me a getFrameAtTime: videoFrame is a NULL pointer error. When I don't use this option, it goes through keyframes.
Maybe possible solutions:
approach: Record video with more keyframes. If I would record the video with an higher keyframe rate in my android app, could that be a solution? Is this possible?
approach (I haven't tried!): I've heard something of video player 3rd party libraries for android that use FFMPEG. Is there any library that can seek while in pause state and update the position for the video on the screen?
Have you tried FFmpegMediaMetadataRetriever?

Categories

Resources