android camera failed when using the camera api - android

I'm having trouble taking a picture in android using the camera api. Using the camera intent seems to work fine, but not when I call the api directly.
Error:
java.lang.RuntimeException: takePicture failed at android.hardware.Camera.native_takePicture
Code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraId = findFrontFacingCamera();
camera = Camera.open(cameraId);
Parameters params = camera.getParameters();
SurfaceView dummy=new SurfaceView(context);
try {
camera.setPreviewDisplay(dummy.getHolder());
camera.startPreview();
camera.takePicture(null, photoHandler, photoHandler);
} catch (IOException e) {
camera.stopPreview();
camera.release();
}
}
private PictureCallback photoHandler = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
}
}
Manifest
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
log cat output
07-17 10:16:02.523: D/MakePhotoActivity(14591): Camera found
07-17 10:16:02.773: D/dalvikvm(14591): threadid=1: still suspended after undo (sc=1 dc=1)
07-17 10:16:18.229: D/AndroidRuntime(14591): Shutting down VM
07-17 10:16:18.229: W/dalvikvm(14591): threadid=1: thread exiting with uncaught exception (group=0x40c4f930)
07-17 10:16:18.309: E/AndroidRuntime(14591): FATAL EXCEPTION: main
07-17 10:16:18.309: E/AndroidRuntime(14591): java.lang.RuntimeException: Unable to start activity ComponentInfo{tv.fakelove.stationtostation/tv.fakelove.stationtostation.MainActivity}: java.lang.RuntimeException: takePicture failed
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.os.Looper.loop(Looper.java:137)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-17 10:16:18.309: E/AndroidRuntime(14591): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 10:16:18.309: E/AndroidRuntime(14591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-17 10:16:18.309: E/AndroidRuntime(14591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-17 10:16:18.309: E/AndroidRuntime(14591): at dalvik.system.NativeStart.main(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591): Caused by: java.lang.RuntimeException: takePicture failed
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.native_takePicture(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.takePicture(Camera.java:1095)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.hardware.Camera.takePicture(Camera.java:1040)
07-17 10:16:18.309: E/AndroidRuntime(14591): at tv.fakelove.stationtostation.MainActivity.onCreate(MainActivity.java:59)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.Activity.performCreate(Activity.java:5104)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-17 10:16:18.309: E/AndroidRuntime(14591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

Check out the answer provided in a previous question: Android: "Camera.takePicture failed" Exception
Apparently you need to start the preview before taking a photo, which includes setting a valid preview surface (which I don't think you are doing).
Also, check out step 5-6 laid out at http://developer.android.com/reference/android/hardware/Camera.html
Obtain an instance of Camera from open(int).
Get existing (default) settings with getParameters().
If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
If desired, call setDisplayOrientation(int).
Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will
be unable to start the preview.
Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback,
Camera.PictureCallback) to capture a photo. Wait for the callbacks to
provide the actual image data.
After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
Call stopPreview() to stop updating the preview surface.
Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in
onPause() (and re-open() it in onResume()).
One thought - as you're using a dummy SurfaceView, would you need to set the width / height of the SurfaceView, to match the width / height of the preview size of the Camera?
Without the full stack trace, there's not a whole lot more I can discern that's wrong with your code.

Related

react native webview java.lang.NullPointerException

Many times to open the WebView, resulting in a number of WebView, it is estimated that the memory leak, I would like to ask a way to solve it?
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0xa617e908)
uncaughtException java.lang.NullPointerException
java.lang.NullPointerException
at android.webkit.WebViewClassic.loadUrlImpl(WebViewClassic.java:2525)
at android.webkit.WebViewClassic.loadUrlImpl(WebViewClassic.java:2541)
at android.webkit.WebViewClassic.loadUrl(WebViewClassic.java:2534)
at android.webkit.WebView.loadUrl(WebView.java:784)
at cn.nin.common.MyAndroid$MyAsyncTask.onPostExecute(MyAndroid.java:1461)
at cn.nin.common.MyAndroid$MyAsyncTask.onPostExecute(MyAndroid.java:1362)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
You didn't give us enough information. NullPointerException can be thrown for many reasons.That error means that an application attempts to use an object reference that has the null value.

Taking picture from non-activity

I am using dummy surface in my code.It's working fine in Canvas HD running 4.2.1 but when the same app is deployed on my nexus 5/S 3 it gives RunTimeException on camera.takepicture
Here's my code
{
camera = Camera.open(cameraId);
if (camera != null)
{
SurfaceView dummy = new SurfaceView(context);
try {
camera.setPreviewDisplay(dummy.getHolder());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
camera.takePicture(null, null, new PhotoHandler(context));
}
Logcat:
07-17 22:46:49.281: E/AndroidRuntime(481): FATAL EXCEPTION: main
07-17 22:46:49.281: E/AndroidRuntime(481): Process: com.example.ex, PID: 481
07-17 22:46:49.281: E/AndroidRuntime(481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ex/com.example.ex.MainActivity}: java.lang.RuntimeException: takePicture failed
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.os.Handler.dispatchMessage(Handler.java:102)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.os.Looper.loop(Looper.java:136)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-17 22:46:49.281: E/AndroidRuntime(481): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 22:46:49.281: E/AndroidRuntime(481): at java.lang.reflect.Method.invoke(Method.java:515)
07-17 22:46:49.281: E/AndroidRuntime(481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-17 22:46:49.281: E/AndroidRuntime(481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-17 22:46:49.281: E/AndroidRuntime(481): at dalvik.system.NativeStart.main(Native Method)
07-17 22:46:49.281: E/AndroidRuntime(481): Caused by: java.lang.RuntimeException: takePicture failed
07-17 22:46:49.281: E/AndroidRuntime(481): at android.hardware.Camera.native_takePicture(Native Method)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.hardware.Camera.takePicture(Camera.java:1245)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.hardware.Camera.takePicture(Camera.java:1190)
07-17 22:46:49.281: E/AndroidRuntime(481): at com.example.ex.MainActivity.capturephoto(MainActivity.java:63)
07-17 22:46:49.281: E/AndroidRuntime(481): at com.example.ex.MainActivity.onCreate(MainActivity.java:26)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.Activity.performCreate(Activity.java:5231)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-17 22:46:49.281: E/AndroidRuntime(481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
After googling and searching many questions on stackoverflow I found this and this but both snippets are used in activties .
How could I use such code in my app so that I can capture picture from background
You can use a dummy SurfaceTexture instead of a SurfaceView. This is reliable and works fine on the old camera API:
mDummyTexture = new SurfaceTexture(1); // pick a random argument for texture id
mCamera.setPreviewTexture(mDummyTexture);
mCamera.startPreview();
mCamera.takePicture(...);
Just make sure to keep mDummyTexture as a member of your class, or you may get abandoned surface errors.
As long as you don't call SurfaceTexture#updateTexImage(), you do not need an OpenGL context.
You shouldn't use a dummy SurfaceView as Android checks is the SurfaceView is displayed on the screen.
Instead, display the preview on a SurfaceView with WindowManager, with type SYSTEM_OVERLAY. Set your SurfaceView width and height to 1px and the opacity to 0 to hide the preview, and that's it!
If you plan to support only 5.0+ devices, or it's a new app that won't be available too soon, you should consider using the new android Camera2 API, which fixed many mistakes of the older API.
Hope it will help you :)
This is not possible, you need an activity to display the camera in.
Consider a transparent activity that just holds the camera view.

Exception while invoking HP ePrint PDF Preview Activity

I am trying to get my android app invoke HP ePrint PDF Preview Activity.
Intent intent = new Intent ();
intent.setPackage("com.hp.android.print");
intent.setClassName("com.hp.android.print", "com.hp.android.print.preview.PdfPreviewActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse("http://www.pdf995.com/samples/pdf.pdf");
intent.setDataAndType( uri, "application/pdf" );
if (intent.resolveActivity(getPackageManager()) != null)
{
getBaseContext().startActivity(intent);
}
I am getting the below error (taken from Logcat of the device):
2014-03-13 03:02:20.915 D 11119/AndroidRuntime: Shutting down VM
2014-03-13 03:02:20.915 W 11119/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40a491f8)
2014-03-13 03:02:20.925 E 11119/AndroidRuntime: FATAL EXCEPTION: main
**java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hp.android.print/com.hp.android.print.preview.PdfPreviewActivity}:** java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
**Caused by: java.lang.NullPointerException
at com.hp.android.print.utils.UriUtils.normalizeIntent(UriUtils.java:133)**
at com.hp.android.print.preview.PdfPreviewActivity.onContinueCreate(PdfPreviewActivity.java:123)
at com.hp.android.print.preview.PrintPreview.handleExternalReceivedIntent(PrintPreview.java:261)
at com.hp.android.print.preview.PrintPreview.onCreate(PrintPreview.java:181)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more
2014-03-13 03:02:20.935 W 132/ActivityManager: Force finishing activity com.hp.android.print/.preview.PdfPreviewActivity
2014-03-13 03:02:20.955 W 132/ActivityManager: Force finishing activity com.example.droidprinttest/.MainActivity
HP ePrint works fine when used manually. Any idea about a fix?
[EDIT] My Goal is to get a PDF file printed on the fly, a solution without preview is okay for me too.

Lib not found error in tesseract [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am working with OCR android. Got samples from googling and work with android tesseract. I have the project as library and refered in another project but, when I run the project it shows the following in Logcat
07-17 10:38:47.092: ERROR/AndroidRuntime(426): FATAL EXCEPTION: main
07-17 10:38:47.092: ERROR/AndroidRuntime(426): java.lang.ExceptionInInitializerError
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at com.imagetotext.ImagetoText.onCreate(ImagetoText.java:21)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.os.Looper.loop(Looper.java:123)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at java.lang.reflect.Method.invoke(Method.java:521)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at dalvik.system.NativeStart.main(Native Method)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): Caused by: java.lang.UnsatisfiedLinkError: Library liblept not found
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at java.lang.Runtime.loadLibrary(Runtime.java:461)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at java.lang.System.loadLibrary(System.java:557)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(TessBaseAPI.java:47)
07-17 10:38:47.092: ERROR/AndroidRuntime(426): ... 14 more
07-17 10:38:47.112: WARN/ActivityManager(60): Force finishing activity com.imagetotext/.ImagetoText
I think there is something wrong with my android.mk. Please help me fix the error.
Exception cause line is,
System.loadLibrary(“lept”);
The problem is liblept.so (shared library) file can not found on specific library path. Without seeing your code just only assumption is your code trying to load shared library liblept.so and the library is not available at that path.
Also the code you are using is either have that liblept.so file in any lib or internal package directory or you have to generate (build) that shared library by using Android-NDK.

Byte array image to bitmap

I'm trying to convert the byte array image to bitmap, but after conversion it gives me black image, why?
// Camera arg conversion to Bitmap
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(arg0, 0,
arg0.length);
Bitmap background = Bitmap.createBitmap(cameraBitmap.getWidth(),
cameraBitmap.getHeight(), Bitmap.Config.ARGB_8888);
I want the camera image converted to bitmap. Any suggestion. Thanks in advance!!
07-17 02:22:18.149: E/AndroidRuntime(398): FATAL EXCEPTION: main
07-17 02:22:18.149: E/AndroidRuntime(398): java.lang.IllegalArgumentException: only support ImageFormat.NV21 and ImageFormat.YUY2 for now
07-17 02:22:18.149: E/AndroidRuntime(398): at android.graphics.YuvImage.<init>(YuvImage.java:82)
07-17 02:22:18.149: E/AndroidRuntime(398): at com.exercise.AndroidCamera.AndroidCamera$4.onPictureTaken(AndroidCamera.java:225)
07-17 02:22:18.149: E/AndroidRuntime(398): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:320)
07-17 02:22:18.149: E/AndroidRuntime(398): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 02:22:18.149: E/AndroidRuntime(398): at android.os.Looper.loop(Looper.java:123)
07-17 02:22:18.149: E/AndroidRuntime(398): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 02:22:18.149: E/AndroidRuntime(398): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 02:22:18.149: E/AndroidRuntime(398): at java.lang.reflect.Method.invoke(Method.java:521)
07-17 02:22:18.149: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 02:22:18.149: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 02:22:18.149: E/AndroidRuntime(398): at dalvik.system.NativeStart.main(Native Method)
Unfortunately, the camera's image format is not ARGB_8888. Most likely it's something like YUV420p or maybe even the JPEG-compressed data.
First of all, determine what is your case.
You have two options: for YUV (which is an interleaved format) use some conversion format and for JPEG create a memorystream for your arg0 array and read the Bitmap from it.
There are similar questions here: BitmapFactory null issue in android
And there is even a solution here: Android byte[] to image in Camera.onPreviewFrame
EDIT: you just have to mangle the bytes in your array a little.
This wiki article explains how to convert the YUV422/420 to YUY2, required by the Android API.
Search for the "Y'UV422 can also be expressed in YUY2 FourCC format code" substring there.

Categories

Resources