I have a tabHost with three activities, one of them is meant for taking pictures and uses the android camera. When I click on this tab for the first time, a blank screen appears for 1-2 seconds before the tabs reappear and the camera preview starts. Is there a way to reduce the delay or at least let the tabs stay while the camera is opened. I tried initialising the camera in a separate thread, but it gave me a runtime exception.
As I understand, you are starting the Camera intent from your application, or are you just displaying a preview of the Camera feed right on your app? Providing code with your question will help you get better answers.
Are you having this problem on the emulator? If so, it's normal behavior and you shouldn't be worried. If this is on an actual device, I don't think you can really do anything about it. Try loading the Camera application by itself and compare times with your application, it shouldn't differ by much.
Ryan
Related
I'm running into an issue and I would appreciate some guidance. I'm writing a small Android program in Android Studio that uses MediaProjection to grab a screenshot of the whole screen and then I want to pass that screenshot as a Bitmap to my System Overlay (based on the chatheads example). When the MediaProjection runs and the Imagereader creates the bitmaps, I'm passing the bitmap into a class global variable so I can pass it into my System Overlay service and display it. The problem I'm running into is if I "tab out" to a different app, my methods stop working from the MainActivity. When my app is in focus, everything works well, when a different app is focused, the system overlay contracts and expands like it should, but the screenshots are not being taken anymore. My understand is its because my app becomes "asleep" since it's not in focus. Could anyone recommend a solution? Can I momentarily awake my mainActivity so it can take a screenshot? Can I keep my activity from falling asleep on app switch? Can I bring focus to my app, take the screenshot and then switch focus to the previous app? Any other ways?
My code can be found here: https://github.com/asheron21/FacebookLikeChathead
MainActivity:https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/MainActivity.java
Service: https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/ChatHeadService.java
Global Variables:https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/Globals.java
Thanks!
I am working on a camera implementation similar to what Snapchat offers.
I have created a subclass of SimpleCameraHost to customize the directories and file paths and to handle saveImage(I am cropping the image into a square before I save it to disk). I have also subclassed CameraFragment as you(Mark) have in the demo.
I need to take three pictures(Take pic...user okays it...Move onto second pic..repeat...) before I continue with the flow of the app. I am using the single shot mode. In the Oncreate() of the DemoFragment
SimpleCameraHost.Builder builder =
new SimpleCameraHost.Builder(new MyCameraHost(getActivity(), useFFC));
setHost(builder.useSingleShotMode(true).build());
Question : I need to know how to prevent the cameraview from restarting
the camera every time I pause the app while I'm on the 'okay the pic page'
and resume.Also if I leave the app idle on this page for a while, it seems to freeze and my camera cannot be started again(from any camera app including the default...I have to restart the device). I reckon this happens because I have not released the camera. Could you please direct me as to how I should handle pausing and resuming the app.
I need to know how to prevent the cameraview from restarting the camera every time I pause the app while I'm on the 'okay the pic page' and resume
In the demo app, if I switch to single-shot mode, take a landscape picture using the rear-facing camera, press HOME from the photo-preview screen, return to the app from the recent-tasks list, and press BACK, everything behaves fine, at least on the Nexus 4 that I just tried it on.
Hence, I would suggest that you compare your app to the demo app and see where things differ that might cause this behavior.
If you feel that there is a bug in the library, feel free to file an issue, but you would need to give me a complete set of materials to be able to reproduce the problem. That would include details of the device (e.g., Build.PRODUCT), the code to reproduce it (if it is not one of the demo apps), and the steps I should manually follow to reproduce it.
Also if I leave the app idle on this page for a while, it seems to freeze and my camera cannot be started again(from any camera app including the default...I have to restart the device)
Once again, I cannot reproduce this behavior. Immediately following the steps that I outlined above, I took another picture in single-shot mode, and left the app on the photo-preview screen while I composed the first part of this answer. I just pressed BACK, and everything is normal. So, again:
Compare your app to the demo app, to see if you can determine what you may be doing that might cause this effect
File an issue if you think that it is a bug in the library, but only do so if you can give me steps to reproduce the bug
Is it possible to record screen video of current running activity from same activity ?
I know how to take screenshot of current activity but don't have any idea about taking screen video record. How would I start with it ? I don't know how to start it.
There are no APIs yet in Android to do this directly. However, you could take screenshots of the app, and then merge them to make a video out of the images.
However, this process is quite tedious, and would probably take a lot of research and investigation.
Since you know how to take screenshots, a good starting point would be to look for libraries/APIs that can take in a series of images, and convert them into a video.
ffmpeg/JMF could be two good starting points.
I have doing a basic object detection on the camera preview screen in Android (greater than 3.2). For the devices which do not support processing on preview screen, I am buffering the preview screen, processing it and clearing the buffer. This part is working as desired.
What I now want is this app to run in the background while any other app is running in the foreground. I am using android service and am able to run a small test app in the background. However my concern is with the camera preview app.
I don't want to display the preview screen but use the preview screen information for processing. This might be too much to ask, but I wanted to know if this is even possible. I came across this link which shows some hope. Basically I want to process the video (preview) stream without displaying it on the screen. If this is doable, then I can think of putting this app in the background and some other app in the foreground.
Unfortunately I won't be able to share the code, however it is the standard logic of creating a surface view and starting the preview.
I would really appreciate any insight into this.
Check comments here.Basically he opens camera hardware, set preview callback and do startpreview without setting the previewDisplay (this might not work on every device). You can try this from your background service. All this will work if your foreground doesn't access the camera app. Please update this if it works. I am interested to know.
I want to write an activity that:
Shows the camera preview (viewfinder), and has a "capture" button.
When the "capture" button is pressed, takes a picture and returns it to the calling activity (setResult() & finish()).
Are there any complete examples out there that works on every device? A link to a simple open source application that takes pictures would be the ideal answer.
My research so far:
This is a common scenario, and there are many questions and tutorials on this.
There are two main approaches:
Use the android.provider.MediaStore.ACTION_IMAGE_CAPTURE event. See this question
Use the Camera API directly. See this example or this question (with lots of references).
Approach 1 would have been perfect, but the issue is that the intent is implemented differently on each device. On some devices it works well. However, on some devices you can take a picture but it is never returned to your app. On some devices nothing happens when you launch the intent. Typically it also saves the picture to the SD card, and requires the SD card to be present. The user interaction is also different on every device.
With approach 2 the issues is stability. I tried some examples, but I've managed to stop the camera from working (until a restart) on some devices and completely freeze another device. On another device the capture worked, but the preview stayed black.
I would have used ZXing as an example application (I work with it a lot), but it only uses the preview (viewfinder), and doesn't take any pictures. I also found that on some devices, ZXing did not automatically adjust the white balance when the lighting conditions changed, while the native camera app did it properly (not sure if this can be fixed).
Update:
For a while I used the camera API directly. This gives more control (custom UI, etc), but I would not recommend it to anyone. I would work on 90% of devices, but every now and again a new device would be released, with a different problem.
Some of the problems I've encountered:
Handling autofocus
Handling flash
Supporting devices with a front camera, back camera or both
Each device has a different combination of screen resolution, preview resolutions (doesn't always match the screen resolution) and picture resolutions.
So in general, I'd not recommend going this route at all, unless there is no other way. After two years I dumped by custom code and switched back to the Intent-based approach. Since then I've had much less trouble. The issues I've had with the Intent-based approach in the past was probably just my own incompetence.
If you really need to go this route, I've heard it's much easier if you only support devices with Android 4.0+.
With approach 2 the issues is stability. I tried some examples, but I've managed to stop the camera from working (until a restart) on some devices and completely freeze another device. On another device the capture worked, but the preview stayed black.
Either there is a bug in the examples or there is a compatibility issue with the devices.
The example that CommonsWare gave works well. The example works when using it as-is, but here are the issues I ran into when modifying it for my use case:
Never take a second picture before the first picture has completed, in other words PictureCallback.onPictureTaken() has been called. The CommonsWare example uses the inPreview flag for this purpose.
Make sure that your SurfaceView is full-screen. If you want a smaller preview you might need to change the preview size selection logic, otherwise the preview might not fit into the SurfaceView on some devices. Some devices only support a full-screen preview size, so keeping it full-screen is the simplest solution.
To add more components to the preview screen, FrameLayout works well in my experience. I started by using a LinearLayout to add text above the preview, but that broke rule #2. When using a FrameLayout to add components on top of the preview, you don't have any issues with the preview resolution.
I also posted a minor issue relating to Camera.open() on GitHub.
"the recommended way to access the camera is to open Camera on a separate thread". Otherwise, Camera.open() can take a while and might bog down the UI thread.
"Callbacks will be invoked on the event thread open(int) was called from". That's why to achieve best performance with camera preview callbacks (e.g. to encode them in a low-latency video for live communication), I recommend to open camera in a new HandlerThread, as shown here.