Next, let's take a look at how to use this interface in the camera application. First, let's take a look at the onCreate method in the Android programming environment. After reading this article, you will certainly have a lot of GAINS, I hope this article will teach you more things.
The former smartphone has many powerful functions, such as cameras, GPS, and wireless Internet access. Now is the time for us to fully use these functions. In this article, we will learn how to take photos of Google Android cameras in the simplest way in the Google Android programming environment.
- super.onCreate(icicle);
-
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
-
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- setContentView(R.layout.camera);
-
- mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
-
- mSurfaceHolder = mSurfaceView.getHolder();
-
- mSurfaceHolder.addCallback(this);
-
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-
- }
In this example, we need to use two files: Layout files and Activity files. A few days ago, the new version of the Android programming environment (codenamed cupcake) was released, with many security improvements. One of them is related to camera permission control. Before that, you can create an app that allows you to take photos without the user's permission. Now this problem has been fixed. If you want to use a camera in your application, add the following code to AndroidManifest. xml:
- super.onCreate(icicle);
-
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
-
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- setContentView(R.layout.camera);
-
- mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
-
- mSurfaceHolder = mSurfaceView.getHolder();
-
- mSurfaceHolder.addCallback(this);
-
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-
- }
This is the basis of the development work, that is, how many auxiliary elements we want to add to the application, such as camera function buttons. In this article, we adopt the simplest method. Apart from taking photos, there is no additional camera function. Next, let's take a look at the layout file camera_surface.xml used in this example ".
Tip: Remember not to use uppercase letters in the resource file name. If you name the file "CameraSurface. xml", it will cause unnecessary trouble. This layout is very simple. There is only one LinearLayout view group, and there is only one SurfaceView under it, that is, our camera screen.
This method allows the camera to prepare for taking a photo, set its parameters, and start previewing the screen in the Android programming environment. I used a "semaphore" parameter to prevent conflict: When mPreviewRunning is true, it means that the camera is active and not disabled, so we can use it.
In this way, we stop the camera and release related resources. As you can see, here we set mPreviewRunning to false. To prevent conflicts in the surfaceChanged method. Why? This means that we have disabled the camera, and we cannot set its parameters or start image preview in the camera.