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