Android Camera Background photo

Source: Internet
Author: User

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:

  1. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=". Mycamera " >
  6. <!--preview box, 0.1 and long width
  7. <Surfaceview
  8. android:id="@+id/camera_surfaceview"
  9. android:layout_width="0.1DP"
  10. android:layout_height="0.1DP" >
  11. </surfaceview>
  12. </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:

  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="Com.chenww.camera.ui"
  4. android:versioncode="1"
  5. android:versionname="1.0" >
  6. <uses-sdk
  7. android:minsdkversion="Ten"
  8. android:targetsdkversion="> "
  9. </uses-sdk>
  10. <!--call camera permissions --
  11. <uses-permission android:name="Android.permission.CAMERA" />
  12. <uses-feature android:name="Android.hardware.camera" />
  13. <uses-feature android:name="Android.hardware.camera.autofocus" />
  14. <!--read and write SD card permissions --
  15. <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE" />
  16. <application
  17. android:allowbackup="true"
  18. android:icon="@drawable/ic_launcher"
  19. android:label="@string/app_name"
  20. android:theme="@style/apptheme" >
  21. <!--photo screen activity --
  22. <activity
  23. android:name="com.chenww.camera.ui.CameraActivity"
  24. android:theme="@android: Style/theme.translucent" >
  25. <!--theme set to transparent --
  26. <intent-filter>
  27. <action android:name="Android.intent.action.MAIN" />
  28. <category android:name="Android.intent.category.LAUNCHER" />
  29. </intent-filter>
  30. </Activity>
  31. </Application>
  32. </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.

    1. No title
    2. Requestwindowfeature (Window.feature_no_title);
    3. Fullscreen
    4. GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
    5. WindowManager.LayoutParams.FLAG_FULLSCREEN);


Binding Surfaceview Initialization Surfaceholder

    1. Initialize Surfaceview
    2. Mysurfaceview = (Surfaceview) Findviewbyid (R.id.camera_surfaceview);
    3. Initialize Surfaceholder
    4. Myholder = Mysurfaceview.getholder ();
    5. 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)

    1. The Mycamera here is the already initialized camera object
    2. 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.

    1. Mycamera.startpreview ();
    2. Auto Focus
    3. Mycamera.autofocus (Myautofocus);
    4. Photo after focus
    5. 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:

    1. AF callback function (NULL implementation)
    2. Private Autofocuscallback Myautofocus = new Autofocuscallback () {
    3. @Override
    4. public void Onautofocus (boolean success, Camera camera) {
    5. }
    6. };

Mypiccallback:

    1. Photo Success callback function
    2. Private Picturecallback Mypiccallback = new Picturecallback () {
    3. @Override
    4. public void Onpicturetaken (byte[] data, camera camera) {
    5. }
    6. }


After processing the data returned by the camera, remember to close the activity and release the resources

    1. Close activity when you finish taking pictures
    2. Mycameraactivity.  This.finish ();
    3. Mycamera.stoppreview ();
    4. Mycamera.release ();
    5. 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

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.