SurfaceView Android camera photo

How to use the most simple Ways to realize the Google Android camera to take pictures.

In the example of this article, we need to use To two files: the layout file and the Activity file.
Tips, numbers A few days ago, the new version of Android 1.5 (codenamed cupcake) was released. There are many improvements in security, one of which is related to camera permission control . Before that, you can create apps that can take pictures without user permission. Now the problem has been fixed. If you want to use the camera in your own application, you need to add the following code in AndroidManifest.xml:   
1. Set the camera layout
 This is the basis of development work, that is to say how many auxiliary elements we hope to add to the application, such as the camera Various function buttons, etc.
In this article we Take the simplest way, in addition to taking pictures, there is no extra camera function. Let’s take a look at the layout file “camera_surface.xml” that will be used in the example of this article.
Tips: Remember Do not use capital letters in the name of the resource file. If you name the file “CameraSurface.xml”, it will bring you unnecessary trouble.
The layout is very simple , There is only one LinearLayout view group, and there is only one SurfaceView view below it, which is our camera screen. Camera implementation code
  now We have already looked at the xml code of the camera, let’s look at the Android code. Let us create an Activity class called “CameraView” to implement the SurfaceHolder.Callback interface:  
 public class CamaraView extends Activity implements SurfaceHolder.Callback  
 The interface SurfaceHolder.Callback is used to receive the information of the camera preview interface change. It implements three methods:  
 surfaceChanged    This method is called when the format and size of the preview interface change.
 surfaceCreated    first instantiation , This method is called when the preview interface is created.
 surfaceDestroyed    as the preview interface This method is called when it is closed.
Let’s take a look below Let’s take a look at how to use this interface in a camera application. First look at the onCreate method in the Activity class.
 super.onCreate( icicle);  
 getWindow ().setFormat(PixelFormat.TRANSLUCENT);   
requestWindowFeature(Window.FEATURE_NO_TITLE);   
getWindow().setFlag(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);

style=”font-family:Tahoma; font-size:12px; color:#31144; line-height:20px; letter-spacing:2px”>  }   
Let’s explain the code one by one:  
 getWindow().setFormat(PixelFormat.TRANSLUCENT);   
requestWindowFeature(Window.FEATURE_NO_TITLE);   
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);   

style=”font-family:Tahoma; font-size:12px; color:#31144; line-height:20px; letter-spacing:2px”>Through the above code, we tell the screen two pieces of information:  

 1, the camera preview interface will be displayed in full screen, No “title”;  
 2, the screen format is “translucent”.
 setContentView(R. layout.camera_surface );  
 mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);  
 In the above code, we use setContentView to set the layout of the Activity to the camera_surface we created earlier, create a SurfaceView object, and get it from the xml file.
 mSurfaceHolder = mSurfaceView. getHolder();  
mSurfaceHolder.addCallback(this);   mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
 Through the above code, we get the holder from the surfaceview and add the callback function to “this”.
This means we The operation (activity) will be able to manage this surfaceview.
 Let’s take a look at the callback How to implement the function:   
public void surfaceCreated(SurfaceHolder holder) {  
mCamera = Camera.open();   
mCamera is an object of the “Camera” class.
In the surfaceCreated method We “turn on” the camera. This is how to start it.
 public void surfaceChanged( SurfaceHolder holder, int format, int w, int h )
{  
if (mPreviewRunning) { < /span>
 mCamera.stopPreview() ;  
 }  
 Camera.Para meters p = mCamera.getParameters();  
 p.setPreviewSize(w, h);   
mCamera.setParameters(p);   
try { 
 mCamera.setPreviewDisplay(holder);  
 }
catch (IOException e) {
  e.printStackTrace();
 }  
 mCamera.startPreview();  
 mPreviewRunning = true;  
 }  
 This method prepares the camera for taking pictures, sets its parameters, and starts to start the preview screen on the Android screen. I used a “semaphore” parameter to prevent conflicts: when mPreviewRunning is true, it means that the camera is active and not turned off, so we can use it.
   public void surfaceDestroyed( SurfaceHolder holder) {
mCamera.stopPreview();   
mPreviewRunning = false;   
mCamera.release();   
}  
 With this method, we stop the camera and release related resources. Positive As you can see, we are here Set mPreviewRunning to false to prevent conflicts in the surfaceChanged method. What’s the reason? Because it means that we have turned off the camera, and we can no longer set its parameters or start image preview in the camera.
Let’s take a look at the end The most important method in this example:  
 Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 
 public void onPictureTaken(byte[] imageData, Camera c)
{ 
 }   
};  
 When the photo is taken, this method is called. For example, you can create an OnClickListener on the interface and call the PictureCallBack method when you click on the screen. This method will provide you with the byte array of the image, and then you can use the Bitmap and BitmapFactory classes provided by Android to convert it from the byte array to the image format you want.
 Conclusion I hope this The article is helpful to everyone. If you have a simple understanding of Android programming, you should be able to easily understand the examples in this article.

In the example of this article, we need to use two files: the layout file and the Activity file.

Tips, numbers A few days ago, the new version of Android 1.5 (codenamed cupcake) was released. There are many improvements in security, one of which is related to camera permission control . Before that, you can create apps that can take pictures without user permission. Now the problem has been fixed. If you want to use the camera in your application, you need to add the following code in AndroidManifest.xml:   

1. Set the camera layout

 This is the basis of development work, that is to say how many auxiliary elements we hope to add to the application, such as the camera Various function buttons, etc.

In this article we Take the simplest way, in addition to taking pictures, there is no extra camera function. Let’s take a look at the layout file “camera_surface.xml” that will be used in the example of this article.

Tips: Remember Do not use capital letters in the name of the resource file. If you name the file “CameraSurface.xml”, it will bring you unnecessary trouble.

The layout is very simple , There is only one LinearLayout view group, and there is only one SurfaceView view below it, which is our camera screen. Camera implementation code

 now We have already looked at the xml code of the camera, let’s look at the Android code. Let us create an Activity class called “CameraView” to implement the SurfaceHolder.Callback interface:  

 public class CamaraView extends Activity implements SurfaceHolder.Callback  

 The interface SurfaceHolder.Callback is used to receive the information of the camera preview interface change. It implements three methods:  

 surfaceChanged    This method is called when the format and size of the preview interface change.

 surfaceCreated    first instantiation , This method is called when the preview interface is created.

 surfaceDestroyed    as the preview interface This method is called when it is closed.

Let’s take a look below Let’s take a look at how to use this interface in a camera application. First look at the onCreate method in the Activity class.

 super.onCreate( icicle);  

 getWindow ().setFormat(PixelFormat.TRANSLUCENT);   

requestWindowFeature(Window.FEATURE_NO_TITLE);   

getWindow().setFlag(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  

 setContentView(R.layout.camera);  

 mSurfaceView = (SurfaceView) fin dViewById(R.id.surface_camera);  

 mSurfaceHolder = mSurfaceView.getHolder();  

 mSurfaceHolder.addCallback(this);   mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  }   

Let’s explain the code one by one:  

 getWindow().setFormat(PixelFormat.TRANSLUCENT);   

requestWindowFeature(Window.FEATURE_NO_TITLE);   

getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);   

Through the above code, we tell the screen two pieces of information:  

 1. The camera preview interface will be displayed in full screen without “title”;  

 2, the screen format is “translucent”.

 setContentView(R. layout.camera_surface );  

 mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);  

 In the above code, we use setContentView to set the layout of the Activity to the camera_surface we created earlier, create a SurfaceView object, and get it from the xml file.

 mSurfaceHolder = mSurfaceView. getHolder();  

mSurfaceHolder.addCallback(this);   mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  

 Through the above code, we get the holder from the surfaceview and add the callback function to “this”.

This means we The operation (activity) will be able to manage this surfaceview.

 Let’s take a look at the callback How to implement the function:   

public void surfaceCreated(SurfaceHolder holder) {  

mCamera = Camera.open();   

mCamera is an object of the “Camera” class.

In the surfaceCreated method We “turn on” the camera. This is how to start it.

 public void surfaceChanged( SurfaceHolder holder, int format, int w, int h )

{  

if (mPreviewRunning) { < /span>

 mCamera.stopPreview() ;  

 }  

 Camera.Parameters p = mCamera .getParameters();  

< p> p.setPreviewSize(w, h);   

mCamera.setParameters(p);   < /span>

try { 

 mCamera.setPreviewDisplay(holder);

 }

catch (IOException e) {< /span>

  e.printStackTrace() ;

 }  

 mCamera.startPreview();  

 mPreviewRunning = true;  

 }  

 This method prepares the camera for taking pictures, sets its parameters, and starts to start the preview screen on the Android screen. I used a “semaphore” parameter to prevent conflicts: when mPreviewRunning is true, it means that the camera is active and not turned off, so we can use it.

  public void surfaceDestroyed( SurfaceHolder holder) {

mCamera.stopPreview();   

mPreviewRunning = false;   

mCamera.release();   

}  

 With this method, we stop the camera and release related resources. Positive As you can see, we are here Set mPreviewRunning to false to prevent conflicts in the surfaceChanged method. What’s the reason? Because it means that we have turned off the camera, and we can no longer set its parameters or start image preview in the camera.

Let’s take a look at the end The most important method in this example:  

 Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 

 public void onPictureTaken(byte[] imageData, Camera c)

{ 

 }   

};  

 When taking a picture, this method is called. For example, you can create an OnClickListener on the interface and call the PictureCallBack method when you click on the screen. This method will provide you with the byte array of the image, and then you can use the Bitmap and BitmapFactory classes provided by Android to convert it from the byte array to the image format you want.

 Conclusion I hope this The article is helpful to everyone. If you have a simple understanding of Android programming, you should be able to easily understand the examples in this article.

Leave a Comment

Your email address will not be published.