Android camera no preview photo background photo taking

Source: Internet
Author: User

I believe many people, like me, want to use the background Server to call the camera to take photos without the user's knowledge,

I have found a lot of information on the Internet. It is generally said that photos cannot be taken without previewing. It involves user privacy and is illegal to call the camera... what should I do !!!


Come on, And, don't play an effect with nothing.

Now... set the SurfaceView length and width to 0.1. Didn't you want to preview it? The problem is that the preview box is so small. If you still can see it, there is no way to do it...

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". myCamera "> <! -- In the preview box, the length and width are 0.1 --> <SurfaceView android: id = "@ + id/camera_surfaceview" android: layout_width = "0.1dp" android: layout_height = "0.1dp"> </SurfaceView> </RelativeLayout>


<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. chenww. camera. ui "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 10 "android: targetSdkVersion = "16"> </uses-sdk> <! -- Call camera permissions --> <uses-permission android: name = "android. permission. CAMERA "/> <uses-feature android: name =" android. hardware. camera "/> <uses-feature android: name =" android. hardware. camera. autofocus "/> <! -- Read and Write SD card permission --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <! -- Activity --> <activity android: name = "com. chenww. camera. ui. CameraActivity" android: theme = "@ android: style/Theme. Translucent"> <! -- Theme is transparent --> <intent-filter> <action android: name = "android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>


Next is the CameraActivity. java file. here you have to add the following code at the beginning of oncreate to set the window to full screen without a title.
// No title requestWindowFeature (Window. FEATURE_NO_TITLE); // full screen getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );

Bind SurfaceView to initialize SurfaceHolder

// Initialize surfaceviewmySurfaceView = (SurfaceView) findViewById (R. id. camera_surfaceview); // initialize response = mySurfaceView. getHolder (); myHolder. setType (SurfaceHolder. Callback );

Next, initialize the Camera object and call the setPreviewDisplay function of the object to set the SurfaceHolder object (myHolder here)

// Here myCamera is the initialized Camera object myCamera. setPreviewDisplay (myHolder );

The next step is to take a photo, but remember

MyCamera. startPreview (); // autoFocus myCamera. autoFocus (myAutoFocus); // take a photo of myCamera. takePicture after focusing (null, null, myPicCallback );

The above myAutoFocus and myPicCallback Auto Focus callback functions and the success photo callback functions respectively


// Auto Focus callback function (empty implement) private AutoFocusCallback myAutoFocus = new AutoFocusCallback () {@ Overridepublic void onAutoFocus (boolean success, Camera camera ){}};


// Callback function private PictureCallback myPicCallback = new PictureCallback () {@ Overridepublic void onPictureTaken (byte [] data, Camera camera ){}}

After processing the data returned by Camera, remember to close the Activity and release various resources.

// Close ActivityMyCameraActivity. this. finish (); myCamera. stopPreview (); myCamera. release (); myCamera = null;


PS: Processing photos. As I mentioned in the previous blog, for example, pictures taken by android cameras are landscape pictures... how to make it straight or perform vertical preview: Click to open the link


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.