Http://item.congci.com/item/android-camera-houtai-paizhao
There are many people want to not let the user know, using the Android background service call camera to take pictures, online a lot of information, all talk about not preview can not achieve photo, involving user privacy, belonging to the illegal call camera ... What to do!!!
Once saw a blog post, there is a classic word: nothing is absolute, nothing is not around the past. The next step is to analyze how to circumvent the past, to achieve no preview photography.
Requirements ①: Do not let users see the photo screen
The difficulty: The preview interface must be in an activity, and after the activity is popped, the user again silly, know what you are doing, how to do!!
Idea: Pop-up activity on the popup, I tamper with the activity, let him all transparent, and then a full screen, and no title bar, and nothing pops up to a effect.
Important ②: Do not preview
Difficulty: illegal call camera, how to do!! Error "Take Picture Failed!!"
Train of thought: you want to have surfaceview to preview in activity, then come a surfaceview, can not beat we again to Surfaceview to tamper with is ... Set this surfaceview length and width are 0.1 you do not want to preview it, also, the problem is that the preview box is so small, if you can see there is no way ...
Well, not much to say, the idea has started to work:
First, the activity layout used to take pictures:
- <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 " >
- <!--preview box, 0.1 and long width
- <Surfaceview
- android:id="@+id/camera_surfaceview"
- android:layout_width="0.1DP"
- android:layout_height="0.1DP" >
- </surfaceview>
- </relativelayout>
Next time fully transparent, in the Androidmanifest.xml, the theme is set to Transparent, remember to add call camera permissions, autofocus permissions and read and write SD card permissions:
- <? 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="Ten"
- android:targetsdkversion="> "
- </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 permissions --
- <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" >
- <!--photo screen activity --
- <activity
- android:name="com.chenww.camera.ui.CameraActivity"
- android:theme="@android: Style/theme.translucent" >
- <!--theme set to transparent --
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- </Activity>
- </Application>
- </manifest>
Then the Cameraactivity.java file, where you have to add the following code at the beginning of OnCreate to set the window full screen and untitled.
- No title
- Requestwindowfeature (Window.feature_no_title);
- Fullscreen
- GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
Binding Surfaceview Initialization Surfaceholder
- Initialize Surfaceview
- Mysurfaceview = (Surfaceview) Findviewbyid (R.id.camera_surfaceview);
- Initialize Surfaceholder
- Myholder = Mysurfaceview.getholder ();
- Myholder.settype (surfaceholder.surface_type_push_buffers);
Next, initialize the camera object and call the object's Setpreviewdisplay function to set the Surfaceholder object (here is Myholder)
- The Mycamera here is the already initialized camera object
- Mycamera.setpreviewdisplay (Myholder);
The next step is to take pictures, but remember before Takepicture Startpreview
Here to remind, can take the photo code directly in the OnCreate function, then the activity is not initialized to complete, Surfaceview has not come out, the result is "take ticture failed!!", It's a good idea to create a new thread within OnCreate to take a photo operation, which is generally not recommended for time-consuming operations within the UI thread.
And the camera's open requires a certain event, it is best to call the Open function after the thread to sleep for two seconds.
- Mycamera.startpreview ();
- Auto Focus
- Mycamera.autofocus (Myautofocus);
- Photo after focus
- Mycamera.takepicture (null, null, mypiccallback);
The above Myautofocus and Mypiccallback respectively AF callback function and successful photo callback function
Myautofocus I directly empty implementation, can be directly new interface object, but that code is too messy, simply empty implementation.
Mypiccallback, is to implement the Onpicturetaken function, inside the camera captured photos of the byte data Processing (storage).
The amount .... A bit more nonsense, on the code:
Myautofocus:
- AF callback function (NULL implementation)
- Private Autofocuscallback Myautofocus = new Autofocuscallback () {
- @Override
- public void Onautofocus (boolean success, Camera camera) {
- }
- };
Mypiccallback:
- Photo Success callback function
- Private Picturecallback Mypiccallback = new Picturecallback () {
- @Override
- public void Onpicturetaken (byte[] data, camera camera) {
- }
- }
After processing the data returned by the camera, remember to close the activity and release the resources
- Close activity when you finish taking pictures
- Mycameraactivity. This.finish ();
- Mycamera.stoppreview ();
- Mycamera.release ();
- Mycamera = null;
PS: The processing of photos, I mentioned in the previous blog post, such as the Android camera to get photos are horizontal screen ... To make him straight up. or Vertical preview: Click to open the link
Finally, I made a small backstage photo demo sent up for everyone to learn.
The front camera is automatically called when the program is opened, and if not, the rear camera is called and the captured photo is resized to a vertical screen.
PS: Best right-click Project after importing project Android Tools-->fix Project Properties In addition, I am using 4.1.2 version compile, if need change, right-click Project Properties-->android Select Other version
Android Camera Background photo