Android achieves camera copy Effect

Source: Internet
Author: User

This article introduces how to achieve the camera copy Effect Based on my own development examples, that is, to project the picture taken by the camera as a copy object to the painting paper. You can continue painting on the painting paper. effect 1.
 

 
You can add a license to the camera in the AndroidManifest. xml file in four steps.
<Uses-permissionandroid: name = "android. permission. CAMERA"/>
 

Step 2: Use framelayout in the layout file, so that the two views can be stacked together. Ensure that the sizes and positions of the two views are the same.

 

[Html] <span style = "font-size: 16px;"> <FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<SurfaceView
Android: id = "@ + id/SurfaceView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
<Com. example. artist. PaintView
Android: id = "@ + id/PaintView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</FrameLayout>
</Span>
<Span style = "font-size: 16px;"> <FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<SurfaceView
Android: id = "@ + id/SurfaceView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
<Com. example. artist. PaintView
Android: id = "@ + id/PaintView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</FrameLayout>
</Span>

 


Surfaceview is used to dynamically display the current video of the camera. in step 3, you need to enable the camera and configure it, And then enable preview.

 

[Java] <span style = "font-size: 16px;"> mSurfaceView = (SurfaceView) this. findViewById (R. id. sfView );
MSurfaceHolder = mSurfaceView. getHolder ();
MSurfaceHolder. addCallback (SimplePaintActivity. this );
MSurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
 
MCamera = Camera. open ();
Camera. Parameters parameters = mCamera. getParameters ();
Parameters. setPictureFormat (PixelFormat. JPEG );
Parameters. setPreviewSize (dm. widthPixels, dm. heightPixels );
Parameters. setPictureSize (dm. widthPixels, dm. heightPixels );
MCamera. setParameters (parameters );
Try {
MCamera. setPreviewDisplay (mSurfaceHolder );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
MCamera. startPreview ();
</Span>
<Span style = "font-size: 16px;"> mSurfaceView = (SurfaceView) this. findViewById (R. id. sfView );
MSurfaceHolder = mSurfaceView. getHolder ();
MSurfaceHolder. addCallback (SimplePaintActivity. this );
MSurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );

MCamera = Camera. open ();
Camera. Parameters parameters = mCamera. getParameters ();
Parameters. setPictureFormat (PixelFormat. JPEG );
Parameters. setPreviewSize (dm. widthPixels, dm. heightPixels );
Parameters. setPictureSize (dm. widthPixels, dm. heightPixels );
MCamera. setParameters (parameters );
Try {
MCamera. setPreviewDisplay (mSurfaceHolder );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
MCamera. startPreview ();
</Span>

 


Step 4: Change com. example. artist. paintView's background transparency. PaintView is a self-implemented view. In the onTouchEvent function and onDraw function, the touch response and painting functions are respectively implemented to respond to the user's Painting view, the statements for setting the background translucent are as follows.

MBkColor = Color. argb (100,255,255,255 );

Canvas. drawColor (mBkColor );

In this way, the camera copy effect is achieved!

 


From Peking University-Google Android lab


 

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.