我為朋友寫個行車記錄儀後臺循環錄像源碼公開了

做自媒體的朋友,應該或者可能有這感受,在公開的場合不好意思拍攝,如果是大V可能不會膽怯,對於剛入門的朋友,可能會發生。於是,我想能不能在手機上黑屏狀態下錄像呢?說幹就幹,經過一番折騰,終於實現了,好東西不敢獨享,現分享給大家。

我為朋友寫個行車記錄儀後臺循環錄像源碼公開了

上個啟動頁圖。

我為朋友寫個行車記錄儀後臺循環錄像源碼公開了

簡單說說這個佈局。

頭頂有五項文字,是textview佈局的。

錄像,開始錄像。

停止,停止錄像。

視頻,是錄像的視頻列表。

關於,是打開WebView鏈接我的微博,沒有用,可以刪除。

保存,是對設置集中保存。

看下面,五行單選。

第一行,選擇前後鏡頭。

第二行,選擇預覽大小,最小是1*1像素點,最大是整個屏幕。即是說,當縮小為1*1也就隱藏了。

第三行,鏡頭的方向,根據實際情況設定。

第四行,是視頻的方向,應配合相機鏡頭設定。

第五行,是視頻質量。像頭條,最低要求1280*720。

再看下面,五個editText。

第一個,設置每個錄像的秒數。

第二個,設置文件名。可以留空。

第三個,設置保存視頻的大小空間,當達到該值,自動刪除第一個視頻。

最後兩個是註冊碼和口令,可以不要。

我為朋友寫個行車記錄儀後臺循環錄像源碼公開了

說說它實現的原理。

很簡單,就是一個長駐的懸浮窗口。並對窗口進行大小控制。其餘和普通錄像無異。

在不使用相機和嘜的情況下,可以邊上網邊錄像,或者邊打電話邊錄像。

下面直接上碼。

package com.hx.lxj;

import android.app.*;

import android.content.*;

import android.net.*;

import android.os.*;

import android.view.*;

import android.widget.*;

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener

{

Context ct;

private TextView tv;

private RadioGroup r1;

private RadioButton r11;

private RadioButton r12;

private RadioGroup r2;

private RadioButton r21;

private RadioButton r22;

private RadioButton r23;

private RadioButton r24;

private RadioGroup r3;

private RadioButton r31;

private RadioButton r32;

private RadioButton r33;

private RadioButton r34;

private RadioGroup r4;

private RadioButton r41;

private RadioButton r42;

private RadioButton r43;

private RadioButton r44;

private RadioGroup r5;

private RadioButton r51;

private RadioButton r52;

private RadioButton r53;

private RadioButton r54;

private EditText et1;

private EditText et2;

private EditText et3;

private EditText et4;

private EditText et5;

private String kl2="";

private String kl0="";

private String mac="";

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

DirFile.createAppDir();

//appdir=PreferenceUtils.getPrefString(ct,"appdir","");

/*if(appdir.equals("")){

Intent in=new Intent(this,SelectDir.class);

startActivity(in);

this.finish();

}*/

ct=this;

tv=(TextView)findViewById(R.id.appdir);

String fp=PreferenceUtils.getPrefString(ct,CL.APPDIRKEY,"請選擇保存的路徑");

tv.setText("路徑:"+fp);

r1= (RadioGroup) this.findViewById(R.id.r1);

r1.setOnCheckedChangeListener(this);

r11= (RadioButton) this.findViewById(R.id.r11);

r12= (RadioButton) this.findViewById(R.id.r12);

r2= (RadioGroup) this.findViewById(R.id.r2);

r2.setOnCheckedChangeListener(this);

r21= (RadioButton) this.findViewById(R.id.r21);

r22= (RadioButton) this.findViewById(R.id.r22);

r23= (RadioButton) this.findViewById(R.id.r23);

r24= (RadioButton) this.findViewById(R.id.r24);

r3= (RadioGroup) this.findViewById(R.id.r3);

r3.setOnCheckedChangeListener(this);

r31= (RadioButton) this.findViewById(R.id.r31);

r32= (RadioButton) this.findViewById(R.id.r32);

r33= (RadioButton) this.findViewById(R.id.r33);

r34= (RadioButton) this.findViewById(R.id.r34);

r4= (RadioGroup) this.findViewById(R.id.r4);

r4.setOnCheckedChangeListener(this);

r41= (RadioButton) this.findViewById(R.id.r41);

r42= (RadioButton) this.findViewById(R.id.r42);

r43= (RadioButton) this.findViewById(R.id.r43);

r44= (RadioButton) this.findViewById(R.id.r44);

r5= (RadioGroup) this.findViewById(R.id.r5);

r5.setOnCheckedChangeListener(this);

r51= (RadioButton) this.findViewById(R.id.r51);

r52= (RadioButton) this.findViewById(R.id.r52);

r53= (RadioButton) this.findViewById(R.id.r53);

r54= (RadioButton) this.findViewById(R.id.r54);

et1=(EditText)this.findViewById(R.id.e1);

et2=(EditText)this.findViewById(R.id.e2);

et3=(EditText)this.findViewById(R.id.e3);

et4=(EditText)this.findViewById(R.id.mainEditText1);

mac=BaseApplication.getNewMac();

mac=mac.replace(":","");

et4.setText(mac);

et5=(EditText)this.findViewById(R.id.mainEditText2);

kl0=PreferenceUtils.getPrefString(ct,CL.KLKEY2,"");

et5.setText(kl0);

setPre();

Intent in=this.getIntent();

String s=in.getStringExtra("action");

if(s!=null&&s.equals("stop")){

stopAlarm();

}

}

private void setPre(){

int jt=PreferenceUtils.getPrefInt(ct,"jt",1);

if(jt==2){

r11.setChecked(true);

r12.setChecked(false);

}else{

r12.setChecked(true);

r11.setChecked(false);

}

int pmdx=PreferenceUtils.getPrefInt(ct,"pinmodaxiao",500);

if(pmdx==1){

r21.setChecked(true);

}else if(pmdx==200){

r22.setChecked(true);

}else if(pmdx==500){

r23.setChecked(true);

}else if(pmdx==600){

r24.setChecked(true);

}

//相機角度

int xjjd=PreferenceUtils.getPrefInt(ct,"xjjd",0);

if(xjjd==0){

r31.setChecked(true);

}else if(xjjd==90){

r32.setChecked(true);

}else if(xjjd==180){

r33.setChecked(true);

}else if(xjjd==270){

r34.setChecked(true);

}

//媒體角度

int mtjd=PreferenceUtils.getPrefInt(ct,"mtjd",90);

if(mtjd==0){

r41.setChecked(true);

}else if(mtjd==90){

r42.setChecked(true);

}else if(mtjd==180){

r43.setChecked(true);

}else if(mtjd==270){

r44.setChecked(true);

}

//視頻大小

int spdx=PreferenceUtils.getPrefInt(ct,"spdx",2);

if(spdx==0){

r51.setChecked(true);

}else if(spdx==1){

r52.setChecked(true);

}else if(spdx==2){

r53.setChecked(true);

}else if(spdx==3){

r54.setChecked(true);

}

int ms=PreferenceUtils.getPrefInt(ct,"ms",60);

et1.setText(ms+"");

int nc=PreferenceUtils.getPrefInt(ct,"nc",2000);

et3.setText(nc+"");

String wjm=PreferenceUtils.getPrefString(ct,"wjm","");

et2.setText(wjm);

}

public void click(View view) {

int id=view.getId();

switch(id){

case R.id.set:

int ms=Integer.parseInt(et1.getText().toString());

PreferenceUtils.setPrefInt(ct,"ms",ms);

String wjm=et2.getText().toString();

PreferenceUtils.setPrefString(ct,"wjm",wjm);

int nc=Integer.parseInt(et3.getText().toString());

PreferenceUtils.setPrefInt(ct,"nc",nc);

String kl1=et5.getText().toString();

String zcm=et4.getText().toString();

kl2=kl(mac);

if(kl1.equals("hx")){

et2.setText(kl2);

}else{

PreferenceUtils.setPrefString(ct,CL.ZCMKEY,zcm);

PreferenceUtils.setPrefString(ct,CL.KLKEY1,kl2);

PreferenceUtils.setPrefString(ct,CL.KLKEY2,kl1);

}

ts("設置保存成功");

break;

case R.id.lx:

PreferenceUtils.setPrefInt(ct,"isLx",1);

startAlarm();

//Intent intent3 = new Intent(MainActivity.this, Server.class);

//startService(intent3);

break;

case R.id.appdir:

Intent intent3 = new Intent(MainActivity.this, SelectDir.class);

startActivity(intent3);

finish();

break;

case R.id.stoplx:

//ts("停止");

stopAlarm();

// Intent intent2=new Intent(MainActivity.this, Server.class);

// stopService(intent2);

break;

//錄像列表

case R.id.t3:

String videoPath = DirFile.getVideoPath();

Intent intent = new Intent();

intent.putExtra("path", videoPath);

intent.setClass(MainActivity.this, FileList.class);

startActivity(intent);

break;

case R.id.t4:

Intent it=new Intent(MainActivity.this, WebView1.class);

startActivity(it);

break;

}

}

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

//得到用戶選中的 RadioButton 對象

//RadioButton radioButton_checked= (RadioButton) group.findViewById(checkedId);

//String t1=radioButton_checked.getText().toString();

switch (checkedId){

case R.id.r11:

//前鏡頭

PreferenceUtils.setPrefInt(ct,"jt",2);

//ts("設置為前鏡頭");

break;

case R.id.r12:

//後鏡頭

PreferenceUtils.setPrefInt(ct,"jt",1);

//ts("設置為後鏡頭");

break;

case R.id.r21:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"pinmodaxiao",1);

//ts("預覽大小設置保存成功");

break;

case R.id.r22:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"pinmodaxiao",200);

//ts("預覽大小設置保存成功");

break;

case R.id.r23:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"pinmodaxiao",500);

//ts("預覽大小設置保存成功");

break;

case R.id.r24:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"pinmodaxiao",600);

//ts("預覽大小設置保存成功");

break;

//相機角度

case R.id.r31:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"xjjd",0);

//ts("相機角度設置保存成功");

break;

case R.id.r32:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"xjjd",90);

//ts("相機角度設置保存成功");

break;

case R.id.r33:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"xjjd",180);

//ts("相機角度設置保存成功");

break;

case R.id.r34:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"xjjd",270);

//ts("相機角度設置保存成功");

break;

//媒體角度

case R.id.r41:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"mtjd",0);

//ts("媒體角度設置保存成功");

break;

case R.id.r42:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"mtjd",90);

//ts("媒體角度設置保存成功");

break;

case R.id.r43:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"mtjd",180);

//ts("媒體角度設置保存成功");

break;

case R.id.r44:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"mtjd",270);

//ts("媒體角度設置保存成功");

break;

//視頻大小

case R.id.r51:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"spdx",0);

//ts("視頻質量設置保存成功");

break;

case R.id.r52:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"spdx",1);

//ts("視頻質量設置保存成功");

break;

case R.id.r53:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"spdx",2);

//ts("視頻質量設置保存成功");

break;

case R.id.r54:

//pinmodaxiao

PreferenceUtils.setPrefInt(ct,"spdx",3);

//ts("視頻質量設置保存成功");

break;

}

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

switch (keyCode)

{

case KeyEvent.KEYCODE_VOLUME_UP:

//BaseApplication.ts("up");

int islx3=PreferenceUtils.getPrefInt(ct,"isLx",0);

if(islx3==0){

PreferenceUtils.setPrefInt(ct,"isLx",1);

startAlarm();

}else if(islx3==1){

PreferenceUtils.setPrefInt(ct,"isLx",0);

//Server.stop();

islx3=0;

stopAlarm();

}

return true;

case KeyEvent.KEYCODE_VOLUME_DOWN:

int islx=PreferenceUtils.getPrefInt(ct,"isLx",0);

if(islx==0){

PreferenceUtils.setPrefInt(ct,"isLx",1);

startAlarm();

}else if(islx==1){

PreferenceUtils.setPrefInt(ct,"isLx",0);

//Server.stop();

islx=0;

stopAlarm();

}

return true;

case KeyEvent.KEYCODE_BACK :

finish();

break;

default:

break;

}

return false;

}

public void ts(String str) {

Toast.makeText(ct, str, Toast.LENGTH_LONG).show();

}

private void startAlarm() {

String kl3=kl(mac);

//ts(kl3);

if(kl3.equals(kl0)){

// stopAlarm();

// star service

Intent intent2 = new Intent(MainActivity.this, Server.class);

startService(intent2);

// start alarm

int id = 1;

String LILY_TEST_INTENT = "com.hx.hxtestintent2";

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(LILY_TEST_INTENT);

intent.setData(Uri.parse("content://calendar/calendar_alerts/1"));

intent.setClass(this, Receiver.class);

intent.putExtra("ID", id);

long atTimeInMillis = System.currentTimeMillis();

intent.putExtra("time", atTimeInMillis);

// intent.putExtra(LABEL, label);

// intent.putExtra(TIME, atTimeInMillis);

PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,

PendingIntent.FLAG_CANCEL_CURRENT);

// am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);

am.setRepeating(AlarmManager.ELAPSED_REALTIME,

SystemClock.elapsedRealtime(), 15 * 1000, sender);

}else{

BaseApplication.ts("請向微信wx2310150678\\n索取口令");

}

}

public void stopAlarm() {

int id = 2;

Intent intent2 = new Intent(MainActivity.this, Server.class);

stopService(intent2);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

String LILY_TEST_INTENT = "com.hx.hxtestintent2";

Intent intent = new Intent(LILY_TEST_INTENT);

intent.setClass(this, Receiver.class);

intent.putExtra("ID", id);

intent.setData(Uri.parse("content://calendar/calendar_alerts/1"));

// id = 2;

// Log.i("lily","id = 2");

// intent.putExtra(ID, id);

PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,

PendingIntent.FLAG_NO_CREATE);

if (sender != null) {

am.cancel(sender);

Toast.makeText(MainActivity.this, "停止", Toast.LENGTH_SHORT).show();

}

ActivityManager activityMgr = (ActivityManager) this

.getSystemService(ACTIVITY_SERVICE);

activityMgr.killBackgroundProcesses(getPackageName());

android.os.Process.killProcess(android.os.Process.myPid());

// ActivityManager manager =

// (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

// manager.killBackgroundProcesses("com.hongxin.wcf");

}

//口令返回

public String kl(String mac){

String mac1=mac.replace(":","");

mac1=mac1.toLowerCase();

mac1=mac1.replace("1","Y")

.replace("2","E")

.replace("3","S")

.replace("4","S")

.replace("5","W")

.replace("6","L")

.replace("7","Q")

.replace("8","B")

.replace("9","J")

.replace("0","L")

.replace("a","2")

.replace("b","2")

.replace("c","1")

.replace("e","1")

.replace("d","2")

.replace("f","2")

.replace("g","2")

.replace("h","4")

.replace("i","4")

.replace("j","4")

.replace("k","4")

.replace("l","3")

.replace("m","5")

.replace("n","4")

.replace("o","5")

.replace("p","6")

.replace("r","6")

.replace("s","5")

.replace("t","6")

.replace("u","8")

.replace("v","7")

.replace("w","8")

.replace("x","8")

.replace("y","8")

.replace("z","7");

mac1= new StringBuffer(mac1).reverse().toString();

return mac1;

}

}

上面這個是主程序。

下面是佈局。

<linearlayout>

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FCF9F8">

<linearlayout>

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center">

<textview>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/lx_btn"

android:id="@+id/lx"

android:layout_margin="10dp"

android:onClick="click"/>

<textview>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/stop_btn"

android:id="@+id/stoplx"

android:layout_margin="10dp"

android:onClick="click"/>

<textview>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="視頻"

android:id="@+id/t3"

android:layout_margin="10dp"

android:onClick="click"/>

<textview>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="關於"

android:id="@+id/t4"

android:layout_margin="10dp"

android:onClick="click"/>

<textview>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/set"

android:id="@+id/set"

android:layout_margin="10dp"

android:onClick="click"/>

/<textview>

/<textview>

/<textview>

/<textview>

/<textview>

/<linearlayout>

<scrollview>

android:layout_width="match_parent"

android:layout_height="match_parent">

<linearlayout>

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<textview>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/appdir"

android:text="保存路徑:"

android:onClick="click"/>

<radiogroup>

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/r1">

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="前鏡頭"

android:textColor="#000000"

android:id="@+id/r11"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="後鏡頭"

android:textColor="#000000"

android:id="@+id/r12"

android:textSize="8sp"/>

/<radiobutton>

/<radiobutton>

/<radiogroup>

<radiogroup>

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/r2">

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="小屏"

android:textColor="#000000"

android:id="@+id/r21"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="中屏"

android:textColor="#000000"

android:id="@+id/r22"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="大屏"

android:textColor="#000000"

android:id="@+id/r23"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="最大屏"

android:textColor="#000000"

android:id="@+id/r24"

android:textSize="8sp"/>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiogroup>

<textview>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="相機鏡頭方向"/>

<radiogroup>

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/r3">

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="0度"

android:textColor="#000000"

android:id="@+id/r31"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="90度"

android:textColor="#000000"

android:id="@+id/r32"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="180度"

android:textColor="#000000"

android:id="@+id/r33"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="270度"

android:textColor="#000000"

android:id="@+id/r34"

android:textSize="8sp"/>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiogroup>

<textview>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="媒體方向"/>

<radiogroup>

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/r4">

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="0度"

android:textColor="#000000"

android:id="@+id/r41"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="90度"

android:textColor="#000000"

android:id="@+id/r42"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="180度"

android:textColor="#000000"

android:id="@+id/r43"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="270度"

android:textColor="#000000"

android:id="@+id/r44"

android:textSize="8sp"/>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiogroup>

<textview>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="視頻大小"/>

<radiogroup>

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/r5">

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="512×384"

android:textColor="#000000"

android:id="@+id/r51"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="800×480"

android:textColor="#000000"

android:id="@+id/r52"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="1280×720"

android:textColor="#000000"

android:id="@+id/r53"

android:textSize="8sp"/>

<radiobutton>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="1920×1080"

android:textColor="#000000"

android:id="@+id/r54"

android:textSize="8sp"/>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiobutton>

/<radiogroup>

<edittext>

android:layout_width="match_parent"

android:ems="10"

android:layout_height="wrap_content"

android:textColor="#000000"

android:id="@+id/e1"

android:hint="單個視頻秒數"/>

<edittext>

android:layout_width="match_parent"

android:ems="10"

android:layout_height="wrap_content"

android:textColor="#000000"

android:id="@+id/e2"

android:hint="文件名"/>

<edittext>

android:layout_width="match_parent"

android:ems="10"

android:layout_height="wrap_content"

android:textColor="#000000"

android:id="@+id/e3"

android:hint="保存空間大小(M)"/>

<edittext>

android:layout_width="fill_parent"

android:ems="10"

android:layout_height="wrap_content"

android:hint="註冊碼"

android:id="@+id/mainEditText1"

/>

<edittext>

android:layout_width="fill_parent"

android:ems="10"

android:layout_height="wrap_content"

android:hint="口令"

android:id="@+id/mainEditText2"

/>

<textview>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="© Copyright Ajin All Rights Reserved."

android:gravity="center_horizontal"

android:layout_marginTop="30dp"/>

<textview>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="網站建設,微信:wx2310150678"

android:gravity="center_horizontal"/>

/<textview>

/<textview>

/<edittext>

/<edittext>

/<edittext>

/<edittext>

/<edittext>

/<textview>

/<textview>

/<textview>

/<textview>

/<linearlayout>

/<scrollview>

/<linearlayout>

服務,建立懸浮。

package com.hx.lxj;

import android.annotation.*;

import android.app.*;

import android.content.*;

import android.graphics.*;

import android.hardware.*;

import android.os.*;

import android.os.PowerManager.*;

import android.util.*;

import android.view.*;

import android.view.View.*;

import android.view.WindowManager.*;

import android.widget.*;

import java.math.*;

import java.util.*;

import android.hardware.Camera;

public class Server extends Service implements SurfaceHolder.Callback {

public static final String ACTION_BACK_HOME = "com.byd.recorder.ACTION_BACK_HOME";

public static final String ACTION_SHOW_RECORDER = "com.byd.recorder.ACTION_SHOW_RECORDER";

private static final int NOTIFICATION_DI = 1234;

private WindowManager mWindowManager;

private WindowManager.LayoutParams mLayoutParams;

static SurfaceView mSurfaceView;

LinearLayout relLay;

static SurfaceHolder surfaceHolder;

public static boolean islx2 = false;

public static int islx1= 0;

public int mKuan = 100;

public int mSC = 15000;

PowerManager powerManager = null;

WakeLock wakeLock = null;

public int issb=0;

private LayoutInflater mInflater;

public View sb;

public int h=1;

public Handler handlerAP=null;

/**

* 拍攝

*

* @param intent

* @return

*/

@Override

public IBinder onBind(Intent intent) {

return null;

}

@SuppressWarnings("deprecation")

@Override

public void onCreate() {

super.onCreate();

islx1=PreferenceUtils.getPrefInt(BaseApplication.getContext(),"isLx",0);

this.mKuan = PreferenceUtils.getPrefInt(this,"pinmodaxiao",300);

this.mSC = PreferenceUtils.getPrefInt(this,"ms",60);

this.mSC = mSC * 1000;// 毫秒

powerManager = (PowerManager) this.getApplicationContext()

.getSystemService(Context.POWER_SERVICE);

wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,

"My Lock");

wakeLock.acquire();//亮屏

handlerAP = new Handler() {

@SuppressLint("HandlerLeak")

public void handleMessage(Message msg) {

if (msg.what == 0) {

String o=(String)msg.obj;

BaseApplication.ts(o);

}

}

};// handler

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

return super.onStartCommand(intent, flags, startId);

}

@SuppressLint("NewApi")

@Override

public void onStart(Intent intent, int startid) {

//Toast.makeText(this, "開始", Toast.LENGTH_SHORT).show();

Notification notification = new Notification.Builder(this)

.setContentTitle("錄像")

.setContentText("後臺運行")

.setSmallIcon(R.drawable.ic_launcher)

.setContentIntent(

PendingIntent.getActivity(this, 0, new Intent(this,

WebView1.class), 0)).build();

startForeground(NOTIFICATION_DI, notification);

if (islx1 >=1)

{

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

//LayoutInflater.from(this).inflate(R.layout.lxj_ly_camera, null);

mSurfaceView = new SurfaceView(this);

surfaceHolder = mSurfaceView.getHolder();

DisplayMetrics dm = getResources().getDisplayMetrics();

int screenWidth = dm.widthPixels;

int screenHeight = dm.heightPixels;

/*WindowManager wm = (WindowManager) this

.getSystemService(Context.WINDOW_SERVICE);

int width = wm.getDefaultDisplay().getWidth();

int height = wm.getDefaultDisplay().getHeight();

double bl=height/width;*/

//float bl = screenHeight / screenWidth;

double bl= div(screenHeight,screenWidth,4);

//BaseApplication.ts(bl+"");

if (mKuan >= screenWidth||mKuan==600) {

mKuan = screenWidth;

}

if (mKuan < 1) {

mKuan = 1;

}

h = (int) (mKuan * bl);

if (h < 1) {

h = 1;

}

if (mKuan < 1) {

mKuan = 1;

}

LayoutParams params_sur = new LayoutParams();

params_sur.width = mKuan;

params_sur.height = h;

params_sur.alpha = 255;

mSurfaceView.setLayoutParams(params_sur);

mSurfaceView.getHolder().addCallback(this);

mLayoutParams = new WindowManager.LayoutParams();

mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;

mLayoutParams.format = 1;

mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL

| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

mLayoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;

// mLayoutParams.gravity =Gravity.LEFT|Gravity.TOP;

mLayoutParams.x = 0;

mLayoutParams.y = 0;

// mLayoutParams.width = screenWidth;

// mLayoutParams.height = screenHeight;

mLayoutParams.width = mKuan;

mLayoutParams.height = h;

// mWindowManager.addView(mRecorderView, mLayoutParams);

relLay = new LinearLayout(this);

// LayoutParams params_rel = new LayoutParams();

// params_rel.width = LayoutParams.WRAP_CONTENT;

// params_rel.height = LayoutParams.WRAP_CONTENT;

relLay.setLayoutParams(mLayoutParams);

relLay.addView(mSurfaceView);

mWindowManager.addView(relLay, mLayoutParams); // ����View

mWindowManager.updateViewLayout(relLay, mLayoutParams);

// MyLog.writeTxtToFile("�������","", "");

//BaseApplication.ts("錄製");

mInflater = LayoutInflater.from(BaseApplication.getContext());

sb=mInflater.inflate(R.layout.seekbar, null);

islx2 = true;

relLay.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v){

//

if(issb==0){

issb=1;

LayoutParams mLayoutParams2=mLayoutParams;

mLayoutParams2.width = LayoutParams.FILL_PARENT;

mLayoutParams2.height=LayoutParams.WRAP_CONTENT;

mLayoutParams2.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;

mWindowManager.addView(sb,mLayoutParams2);

mWindowManager.updateViewLayout(sb, mLayoutParams);

SeekBar seekbar = (SeekBar)sb.findViewById(R.id.seekbarSeekBar1);

seekbar.setOnSeekBarChangeListener(

new SeekBar.OnSeekBarChangeListener() {

public void onStopTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

}

public void onStartTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

}

public void onProgressChanged(SeekBar seekBar, int progress,

boolean fromUser) {

// TODO Auto-generated method stub

int progress1 = progress/10;

PreferenceUtils.setPrefInt(BaseApplication.getContext(),"fsu",progress1);

Camera.Parameters p =ThreadC.mCamera.getParameters();

p.setZoom(progress1);

ThreadC.mCamera.setParameters(p);

}

}

);

//停止

TextView tv=(TextView)sb.findViewById(R.id.seekbarTextView1);

tv.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v){

Intent intent = new Intent(BaseApplication.getContext(), MainActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra("action","stop");

PendingIntent pendingIntent =

PendingIntent.getActivity(BaseApplication.getContext(), 0, intent, 0);

try {

pendingIntent.send();

} catch (PendingIntent.CanceledException e) {

e.printStackTrace();

}

}

});

}else{

//BaseApplication.ts("123");

mLayoutParams = new WindowManager.LayoutParams();

mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;

mLayoutParams.format = 1;

mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL

| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

mLayoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;

// mLayoutParams.gravity =Gravity.LEFT|Gravity.TOP;

mLayoutParams.x = 0;

mLayoutParams.y = 0;

// mLayoutParams.width = screenWidth;

// mLayoutParams.height = screenHeight;

mLayoutParams.width = mKuan;

mLayoutParams.height = h;

issb=0;

mWindowManager.removeView(sb);

mWindowManager.updateViewLayout(relLay, mLayoutParams);

}

}

});

}// islx

}

/**

* 提供(相對)精確的除法運算。當發生除不盡的情況時,由scale參數指

* 定精度,以後的數字四捨五入。

* @param v1 被除數

* @param v2 除數

* @param scale 表示表示需要精確到小數點以後幾位。

* @return 兩個參數的商

*/

public static double div(double v1, double v2, int scale) {

if (scale < 0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* �������ʱ�Ļص�

*/

@SuppressWarnings("deprecation")

@Override

public void onDestroy() {

try {

mWindowManager.removeView(relLay);

islx2 = false;

ThreadC thread = ThreadC.thread;

if (thread != null) {

thread.isStop = false;

thread.stopRecord();

thread.stop();

//BaseApplication.ts("停止");

}

} catch (Exception e) {

//Err.getErrStr(e);

DirFile.err(e);

}

super.onDestroy();

}

public static void stop(){

ThreadC thread = ThreadC.thread;

islx1=0;

if (thread != null) {

thread.isStop = false;

thread.stopRecord();

thread.stop();

//BaseApplication.ts("停止錄像");

}

}

@Override

public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2,

int arg3) {

// TODO �Զ���ɵķ������

surfaceHolder = holder;

//BaseApplication.ts("surfaceChanged");

// MyLog.writeTxtToFile("surfaceChanged", "", "");

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

try {

surfaceHolder = holder;

ThreadC thread = new ThreadC(mSC, mSurfaceView, surfaceHolder, handlerAP,this);

thread.start();

BaseApplication.ts("新建錄像");

} catch (Exception e) {

DirFile.err(e);

//Err.getErrStr(e);

// MyLog.writeTxtToFile("server:"+e.toString(), "", "");

}

}

@Override

public void surfaceDestroyed(SurfaceHolder arg0) {

mSurfaceView = null;

surfaceHolder = null;

islx2 = false;

//BaseApplication.ts("");

}

//-----------------------------------------

public boolean onTouchEvent(MotionEvent event) {

if (event.getPointerCount() == 1) {

handleFocusMetering(event, ThreadC.mCamera);

} else {

float oldDist = 0;

switch (event.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_POINTER_DOWN:

oldDist = getFingerSpacing(event);

break;

case MotionEvent.ACTION_MOVE:

float newDist = getFingerSpacing(event);

if (newDist > oldDist) {

handleZoom(true, ThreadC.mCamera);

} else if (newDist < oldDist) {

handleZoom(false, ThreadC.mCamera);

}

oldDist = newDist;

break;

}

}

return true;

}

private void handleZoom(boolean isZoomIn, Camera camera) {

Camera.Parameters params = camera.getParameters();

if (params.isZoomSupported()) {

int maxZoom = params.getMaxZoom();

int zoom = params.getZoom();

if (isZoomIn && zoom < maxZoom) {

zoom++;

} else if (zoom > 0) {

zoom--;

}

params.setZoom(zoom);

camera.setParameters(params);

} else {

}

}

private static void handleFocusMetering(MotionEvent event, Camera camera) {

Camera.Parameters params = camera.getParameters();

Camera.Size previewSize = params.getPreviewSize();

Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f, previewSize);

Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 1.5f, previewSize);

camera.cancelAutoFocus();

if (params.getMaxNumFocusAreas() > 0) {

List<camera.area> focusAreas = new ArrayList<camera.area>();/<camera.area>/<camera.area>

focusAreas.add(new Camera.Area(focusRect, 800));

params.setFocusAreas(focusAreas);

} else {

}

if (params.getMaxNumMeteringAreas() > 0) {

List<camera.area> meteringAreas = new ArrayList<camera.area>();/<camera.area>/<camera.area>

meteringAreas.add(new Camera.Area(meteringRect, 800));

params.setMeteringAreas(meteringAreas);

} else {

}

final String currentFocusMode = params.getFocusMode();

params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);

camera.setParameters(params);

camera.autoFocus(new Camera.AutoFocusCallback() {

@Override

public void onAutoFocus(boolean success, Camera camera) {

Camera.Parameters params = camera.getParameters();

params.setFocusMode(currentFocusMode);

camera.setParameters(params);

}

});

}

private static float getFingerSpacing(MotionEvent event) {

float x = event.getX(0) - event.getX(1);

float y = event.getY(0) - event.getY(1);

return (float) Math.sqrt(x * x + y * y);

}

private static Rect calculateTapArea(float x, float y, float coefficient, Camera.Size previewSize) {

float focusAreaSize = 300;

int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();

int centerX = (int) (x / previewSize.width - 1000);

int centerY = (int) (y / previewSize.height - 1000);

int left = clamp(centerX - areaSize / 2, -1000, 1000);

int top = clamp(centerY - areaSize / 2, -1000, 1000);

RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);

return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));

}

private static int clamp(int x, int min, int max) {

if (x > max) {

return max;

}

if (x < min) {

return min;

}

return x;

}

//-----------------------------------------

}

錄像線程。

package com.hx.lxj;

import android.annotation.*;

import android.content.*;

import android.graphics.*;

import android.hardware.*;

import android.hardware.Camera.*;

import android.media.*;

import android.os.*;

import android.view.*;

import java.io.*;

import java.text.*;

import java.util.*;

import android.hardware.Camera;

public class ThreadC extends Thread {

private MediaRecorder mediarecorder;//

private SurfaceHolder surfaceHolder;

private SurfaceView surfaceview;//

static Camera mCamera;

private long recordTime;

private Context context;

private long startTime = Long.MIN_VALUE;

private long endTime = Long.MIN_VALUE;

private HashMap<string> map = new HashMap<string>();/<string>/<string>

public boolean exit;

public static final int MEDIA_TYPE_IMAGE = 1;

public static final int MEDIA_TYPE_VIDEO = 2;

static ThreadC thread;

public long nc = 1;

public int jt = 1;

public int jd = 0;

public int cjd = 0;

public int ms = 15000;

public static String wjm = "";

public int int_kuan_p = 1280;

public int int_height_p = 720;

public static String app_dir = "";

public static int sds = 0;

public boolean isStop = true;

public Handler handlerAP=null;

public int islx1=0;

public ThreadC(long recordTime, SurfaceView surfaceview,

SurfaceHolder surfaceHolder,Handler handler, Context context) {

this.recordTime = recordTime;

this.surfaceview = surfaceview;

this.surfaceHolder = surfaceHolder;

this.context = context;

this.handlerAP=handler;

String str_wjm = PreferenceUtils.getPrefString(context,"wjm","");

int str_jt = PreferenceUtils.getPrefInt(context,"jt",1);

int str_jd = PreferenceUtils.getPrefInt(context,"xjjd",0);

int str_cjd = PreferenceUtils.getPrefInt(context,"mtjd",90);

int str_nc = PreferenceUtils.getPrefInt(context,"nc",2000);

int str_sc = PreferenceUtils.getPrefInt(context,"ms",2000);

int str_kuan_p = PreferenceUtils.getPrefInt(context,"spdx",1);

if (!str_wjm.equals("")) {

ThreadC.wjm = str_wjm;

}

if (str_jt>=0) {

this.jt = str_jt;

}

if (str_jd>=0) {

this.jd = str_jd;

}

if (str_cjd>=0) {

this.cjd = str_cjd;

}

if (str_nc>=0) {

this.nc = str_nc;

}

if (str_sc>=0) {

this.ms = str_sc* 1000;// 秒數

}

if (str_kuan_p==0) {

this.int_kuan_p = 521;

this.int_height_p=384;

}else if(str_kuan_p==1){

this.int_kuan_p =800;

this.int_height_p=480;

}else if(str_kuan_p==3){

this.int_kuan_p =1920;

this.int_height_p=1080;

}else{

this.int_kuan_p =1280;

this.int_height_p=720;

}

new AutoFocusCallback() {

public void onAutoFocus(boolean success, Camera camera) {

if (success)// success��ʾ�Խ��ɹ�

{

// myCamera.setOneShotPreviewCallback(null);

}

}

};

}

@Override

public void run() {

islx1=PreferenceUtils.getPrefInt(BaseApplication.getContext(),"isLx",0);

while (isStop && islx1==1) {

try {

if (islx1 ==1 && isStop == true) {

startRecord();

Timer timer = new Timer();

timer.schedule(new TimerThread(), recordTime);

isStop = false;

}else{

break;

}

} catch (Exception e) {

//DirFile.err(e);

break;

}

}

}

/** * ��ȡ����ͷʵ����� * * @return */

@SuppressWarnings("deprecation")

public Camera getCameraInstance() {

mCamera = null;

try {

//MyLog.writeTxtToFile("��ȡ����ͷʵ�����", "", "");

if (jt == 1) {

mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

} else {

mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);

}

mCamera.setDisplayOrientation(cjd);

Camera.Parameters parameters = mCamera.getParameters();

//List<size> list = parameters.getSupportedPictureSizes();/<size>

List<size> list2 = parameters.getSupportedVideoSizes();/<size>

int index = bestVideoSize(list2);

int_kuan_p=list2.get(index).width;

int_height_p=list2.get(index).height;

parameters.setPreviewFrameRate(5); // ÿ��5֡

parameters.setPictureFormat(PixelFormat.JPEG);// ������Ƭ������ʽ

parameters.set("jpeg-quality", 99);// ��Ƭ���

parameters

.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

// camera.autoFocus(myAutoFocusCallback);

mCamera.cancelAutoFocus();// 2���Ҫʵ�������Զ��Խ�����һ��������

//mCamera.setParameters(parameters);

Camera.Parameters p =mCamera.getParameters();

int progress1=PreferenceUtils.getPrefInt(BaseApplication.getContext(),"fsu",0);

p.setZoom(progress1);

mCamera.setParameters(p);

mCamera.startPreview();

} catch (Exception e) {

DirFile.err(e);

}

return mCamera;

}

//

public int bestVideoSize(List<size> videoSizeList){/<size>

Collections.sort(videoSizeList, new Comparator<camera.size>() {/<camera.size>

@Override

public int compare(Camera.Size lhs, Camera.Size rhs) {

if (lhs.width > rhs.width) {

return -1;

} else if (lhs.width == rhs.width) {

return 0;

} else {

return 1;

}

}

});

for(int i=0;i<videosizelist.size>

if(videoSizeList.get(i).width<=int_kuan_p){

return i;

}

}

return 0;

}

/** * ��ʼ¼�� */

public void startRecord() {

try {

//float r1=int_kuan_p/int_height_p;

//if(r1!=16/9){

//int_height_p=int_kuan_p/16*9;

//}

mediarecorder = new MediaRecorder();// ����mediarecorder����

mCamera = getCameraInstance(); // ����camera

mCamera.unlock();

mediarecorder.setCamera(mCamera); // ����¼����ƵԴΪCamera(���)

mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // ����¼���ļ��������ʽ���ֱ���֮�࣬���ȫ�������

//

// if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_1080P)){

// mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));

// }

// else

// if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)){

// mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));

// }

// else if

// (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_HIGH)){

// mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

// }

// mediarecorder.setOutputFile("/sdcard/sForm.3gp");

// �����ļ�·��

mediarecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO)

.toString());

mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

mediarecorder.setVideoSize(int_kuan_p, int_height_p);

//mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

mediarecorder.setVideoFrameRate(30);

mediarecorder.setVideoEncodingBitRate(20 * 1024 * 1024);

mediarecorder.setPreviewDisplay(surfaceHolder.getSurface()); // ������Ƶ�ļ�����·��

mediarecorder.setOrientationHint(jd);

// ׼��¼��

mediarecorder.prepare(); // ��ʼ¼��

mediarecorder.start();

} catch (IllegalStateException e) {

DirFile.err(e);

} catch (IOException e) {

DirFile.err(e);

}

}

public void releaseMediaRecorder() {

if (mediarecorder != null) {

// ���recorder����

mediarecorder.reset();

// �ͷ�recorder����

mediarecorder.release();

mediarecorder = null;

// Ϊ����ʹ��������ͷ

mCamera.lock();

// MyLog.writeTxtToFile("�ͷųɹ�","","");

}

}

/** * ֹͣ¼�� */

public void stopRecord() {

try {

isStop = true;

if (mediarecorder != null) {

mediarecorder.stop();

// time.setText(format(hour) + ":" + format(minute) + ":"

// + format(second));

mediarecorder.release();

mediarecorder = null;

if (mCamera != null) {

mCamera.release();

mCamera = null;

}

checkFiles();

//Message msg=new Message();

//msg.what=0;

//msg.obj="停止錄像了";

//handlerAP.sendMessage(msg);

//Up_down_fun.upload_video();

}

} catch (IllegalStateException e) {

DirFile.err(e);

}

}

public void stopRecord_2() {

try {

if (isStop == true) {

isStop = false;

if (mediarecorder != null) {

mediarecorder.stop();

// time.setText(format(hour) + ":" + format(minute) + ":"

// + format(second));

mediarecorder.release();

mediarecorder = null;

if (mCamera != null) {

mCamera.release();

mCamera = null;

}

checkFiles();

BaseApplication.ts("重新開始");

}

} else {

isStop = true;

thread = new ThreadC(ms, surfaceview, surfaceHolder,

handlerAP,context);

thread.start();

//BaseApplication.ts("錄像了");

}

} catch (IllegalStateException e) {

DirFile.err(e);

}

}

/**

* ��ʽ��ʱ��

*/

public String format(int i) {

String s = i + "";

if (s.length() == 1) {

s = "0" + s;

}

return s;

}

/** * ��ʱ�� * @author bcaiw * */

class TimerThread extends TimerTask {

/** * ֹͣ¼�� */

@Override

public void run() {

try {

stopRecord();

this.cancel();

int il=PreferenceUtils.getPrefInt(BaseApplication.getContext(),"isLx",0);

if(il==1){

thread = new ThreadC(ms, surfaceview, surfaceHolder,

handlerAP, context);

thread.start();

}

} catch (Exception e) {

//Err.err_ts(e);

map.clear();

map.put("recordingFlag", "false");

String ac_time = getVedioRecordTime();// ¼��ʱ��

map.put("recordTime", ac_time);

}

}

}

/**

* ͨ�÷��������ն��̹߳������ݣ��п��ܲ������msg�����Զ���map����

*

* @param handle

* @param iType

* @param info

*/

public void sendMsgToHandle(Handler handle, int iType,

Map<string> info) {/<string>

Message threadMsg = handle.obtainMessage();

threadMsg.what = iType;

Bundle threadbundle = new Bundle();

threadbundle.clear();

for (Iterator<string> i = info.keySet().iterator(); i.hasNext();) {/<string>

Object obj = i.next();

threadbundle.putString(obj.toString(), info.get(obj));

}

threadMsg.setData(threadbundle);

handle.sendMessage(threadMsg);

}

/**

* ���㵱ǰ��¼��ʱ�䣬Ĭ��ֵ����0

*

* @return

*/

public String getVedioRecordTime() {

String result = "0";

if (startTime != Long.MIN_VALUE && endTime != Long.MIN_VALUE) {

long tempTime = (endTime - startTime);

result = String.valueOf(tempTime);

}

return result;

}

public void backStop() {

mediarecorder.stop();

}

@SuppressLint("SimpleDateFormat")

private static File getOutputMediaFile(int type) {

// �ж�SDCard�Ƿ����

// if (!Environment.MEDIA_MOUNTED.equals(Environment

// .getExternalStorageState())) {

// Log.d(TAG, "SDCard������");

// return null;

// }

// File mediaStorageDir = new

// File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),

// "MyCameraApp");

// �������ͼƬ��Ӧ�ó���ж�غ��������ڣ����ܱ�����Ӧ�ó�������˱���λ�������

// �����ڵĻ������������洢Ŀ¼

// File mediaStorageDir = new File(

// Environment.getExternalStorageDirectory() + File.separator

// + "/hx_camera/camera/");

// if (!mediaStorageDir.exists()) {

// if (!mediaStorageDir.mkdir()) {

// //Log.d(TAG, "failed to create directory");

// return null;

// }

// }

// MyLog.writeTxtToFile("�����ļ���ʼ","","");

File mediaFile = null;

try {

File mediaStorageDir = null;

app_dir = DirFile.getVideoPath();

//MyLog.writeTxtToFile("¼���ļ��У�"+app_dir,"","");

String timestamp2 = new SimpleDateFormat("yyyyMMdd")

.format(new Date());

//MyLog.writeTxtToFile(timestamp2,"","");

if (!wjm.equals("")) {// ����ļ�����ڣ�����

wjm = timestamp2 + "_" + wjm;

// mediaStorageDir=new File(app_dir + File.separator +

// wjm+timestamp2+File.separator);

mediaStorageDir = new File(app_dir + File.separator + wjm);

//MyLog.writeTxtToFile("����","","");

} else {

mediaStorageDir = new File(app_dir);

}

if (!mediaStorageDir.exists()) {

if (!mediaStorageDir.mkdirs()) {

return null;

}

}

// ����ý���ļ���yyyyMMdd_

String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss")

.format(new Date());

//MyLog.writeTxtToFile(mediaStorageDir+"/"+timestamp,"","");

if (type == MEDIA_TYPE_IMAGE) {

mediaFile = new File(mediaStorageDir + File.separator + "IMG_"

+ timestamp + ".jpg");

} else if (type == MEDIA_TYPE_VIDEO) {

mediaFile = new File(mediaStorageDir + File.separator

+ timestamp + ".mp4");

} else {

//MyLog.writeTxtToFile("�ļ���������", "", "");

return null;

}

} catch (Exception e) {

//Err.err_ts(e);

DirFile.err(e);

}

return mediaFile;

}

public void checkFiles() {

// -------------------------

new Thread(new Runnable() {

@Override

public void run() {

try {

String filePath1 = DirFile.getVideoPath();

DirFile.del_file_by_size(filePath1,

(long) (nc * 1024 * 1024L));

} catch (Exception e) {

//Err.err_ts(e);

}

}

}).start();

}

}

主要實現過程就是這樣,全部放上太長了,還是老方法,放在網盤,供免費下載。可以私信我。

如果對你有幫助,賞個贊。

如果有人支持,我會繼續寫點其他實例,有興趣的關注一下。

/<videosizelist.size>


分享到:


相關文章: