Android uses two Surfaceview to draw on a camera screen

Source: Internet
Author: User

Reprinted from http://blog.csdn.net/jesse__zhong/article/details/24934083

Using dual surface, set the first to a transparent background, draw an image on the camera, and engage in a day of tangled up.

Which refer to http://blog.csdn.net/yanzi1225627/article/details/7934710 's ideas and code, however, will always appear canvas=holder.lockcanvas (); Returns a case where the canvas is null. Resolves the issue after joining the thread.

Main activity:

public class Mainactivity extends Activity {

Private Svdraw Surfacedraw = null;
Private Surfaceview Surfaceview = null;
Private Surfaceholder holder1 = null;
Private canvas canvas = null;
Private Camera cam = null;
Private Boolean previewrunning = true;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Draw Surface1
  Surfaceview = (Surfaceview) Findviewbyid (R.id.surface1);
Holder1 = Surfaceview.getholder ();
Holder1.addcallback (New Mysurfaceviewcallback ());
Holder1.settype (surfaceholder.surface_type_push_buffers);
Holder1.setfixedsize (500, 350);
Holder1.setformat (pixelformat.transparent);

Canvas = Holder1.lockcanvas ();
if (canvas! = null) {
Canvas.drawcolor (color.transparent);
Paint p = new paint ();
P.setantialias (TRUE);
P.setcolor (color.red);
P.setstyle (Style.stroke);
Canvas.drawpoint (100.0f, 100.0f, p);
Canvas.drawline (0, A, a, p);
}

Drawing Surface2 where threads are used
Surfacedraw = (Svdraw) Findviewbyid (R.id.mdraw);
Surfacedraw.setvisibility (view.visible);
Surfacedraw.drawline ();
}

=============================create surface 1=================================================
Private class Mysurfaceviewcallback implements Surfaceholder.callback {
@Override
public void surfacechanged (surfaceholder holder, int format, int width,
int height) {
}

@SuppressWarnings ("deprecation")
@Override
public void surfacecreated (Surfaceholder holder) {
Cam = Camera.open (); Get the first camera
Parameters param = cam.getparameters ();
Param.setpreviewsize (Display.getwidth (), Display.getheight ());
Param.setpreviewframerate (5); 5 Frames a second
Param.setpictureformat (Pixelformat.jpeg); Picture form
Param.set ("Jpen-quality", 80);
Cam.setparameters (param);
Cam.setdisplayorientation (90); Correct camera automatic rotation, correct angle, if reference, then camera angle deviation 90 degrees

try {
Cam.setpreviewdisplay (holder);
} catch (IOException e) {
}

Cam.startpreview (); To preview
Previewrunning = true; Preview already started
}

@Override
public void surfacedestroyed (Surfaceholder holder) {
if (cam! = null) {
if (previewrunning) {
Cam.stoppreview (); Stop previewing
Previewrunning = false;
}
Cam.release ();
}
}
}
}

/* Just one is inherited view one is Surfaceview, will attributesetattrs plus. Just deal with who is the top layer of view who is set to transparent, the preview video of the Surfaceview is set to the bottom, and in the XML attribute file can be set to visible in the * *
public class Svdraw extends Surfaceview implements surfaceholder.callback{

protected Surfaceholder sh;
private int mwidth;
private int mheight;
Private MyThread thread;

Public Svdraw (context context, AttributeSet Attrs) {
Super (context, attrs);
SH = this.getholder ();
Sh.addcallback (this);
Sh.setformat (pixelformat.transparent);
Setzorderontop (TRUE);
}

public void surfacechanged (Surfaceholder arg0, int arg1, int w, int h) {
Mwidth = W;
Mheight = h;
}

public void surfacecreated (Surfaceholder arg0) {

}

public void surfacedestroyed (Surfaceholder arg0) {

}
void Cleardraw ()
{
Canvas canvas = Sh.lockcanvas ();
Canvas.drawcolor (Color.Blue);
Sh.unlockcanvasandpost (canvas);
}
public void DrawLine ()
{
Draw an image when previewing a video
/*canvas Canvas = Sh.lockcanvas ();
Canvas.drawcolor (color.transparent);
Paint p = new paint ();
P.setantialias (TRUE);
P.setcolor (color.red);
P.setstyle (Style.stroke);
Canvas.drawpoint (100.0f, 100.0f, p);
Canvas.drawline (0,110, p);
Canvas.drawcircle (A, 10.0f, p);
Sh.unlockcanvasandpost (canvas); */

thread = new MyThread (SH);
Thread.setrun (TRUE);
Thread.Start ();
}
}

Drawing threads
public class MyThread extends Thread {
Private Surfaceholder holder;
Private Boolean run;

Public MyThread (Surfaceholder holder) {
This.holder = holder;
Run = true;
}

@Override
public void Run () {
int counter = 0;
Canvas canvas = null;
while (run) {
Specific drawing work
try {
Gets the canvas object and locks the
Canvas = Holder.lockcanvas ();
Set the background color of a canvas object
Canvas.drawcolor (color.transparent);

Create a brush
Paint p = new paint ();
Set Brush color
P.setcolor (color.red);
Set Text Size
P.settextsize (30);

Creates a Rect object rect
Rect rect = new rect (100, 50, 380, 330);
Draw a rect on the canvas
Canvas.drawrect (rect, p);
Show time on canvas
Canvas.drawtext ("Interval =" + (counter++) + "seconds.", +, 410, p);
Thread.Sleep (1000);
} catch (Exception e) {
E.printstacktrace ();
} finally {
if (canvas! = null) {
Unlock and commit the changes
Holder.unlockcanvasandpost (canvas);
}
}
}
}

public Boolean Isrun () {
return run;
}

public void Setrun (Boolean run) {
This.run = run;
}
}

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "com.example.test02"
Android:versioncode= "1"
Android:versionname= "1.0" >

<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"

<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= "Com.example.test02.MainActivity"
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

<!--z>
&nbsp;<uses-feature android:name= "Android.hardware.camera"/>
<uses-feature android:name= "Android.hardware.camera.autofocus"/>
<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

Android uses two Surfaceview to draw on a camera screen

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.