android 中實現(xiàn)網(wǎng)頁調(diào)用攝像頭功能方法·如下:
創(chuàng)新互聯(lián)成立與2013年,先為汪清等服務(wù)建站,汪清等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為汪清企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1. 首先在Manifest.xml中添加如下權(quán)限:
!-- 訪問網(wǎng)絡(luò)權(quán)限 --
uses-permission android:name="android.permission.INTERNET"/
!-- 調(diào)用攝像頭的權(quán)限 --
uses-permission android:name="android.permission.CAMERA"/
!-- 文件系統(tǒng)權(quán)限 --
uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/
!-- SD卡寫權(quán)限 --
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/
2. 編寫index.html放在android項目下的assests目錄下:
index.html:
html
head
titleWebview capture testing.../title
script type="text/javascript"
//android中調(diào)用js方法
function wave() {
alert('cc2');
document.getElementById("droid").innerHTML ="android_waving.png";
}
//android中調(diào)用js方法2,并更id為"droid"的span標(biāo)簽的值及新剛拍的照片顯示到"pic0"里
function wave2(name) {
alert(name);
document.getElementById("droid").innerHTML = "Photopath: " + name;
document.getElementById("pic0").src=name;
}
//html的點擊事件,并通過"window.demo.clickOnAndroid"觸發(fā)android中的clickOnAndroid方法
function clickOnHtml () {
var fileName = window.demo.clickOnAndroid();
//wave2(fileName);
}
//
function revert() {
alert('cc');
document.getElementById("droid").innerHTML ="aa2";
}
/script
style type="text/css"
body{
font:14px;
color:#333;
line-height: 1.8em;
margin: 0;
padding: 0;
width:480px;
}
html{
outline: none;
hide-focus:expression(this.hideFocus=true);
}
/style
/head
body
div
li
pHere is the funtion to TakePhoto in html/p
!-- 通過"window.demo.clickOnAndroid"觸發(fā)android中的clickOnAndroid方法 --
a onClick="window.demo.clickOnAndroid()"TakePhoto/abr/
span id='droid'Photopath:/span
br/
br/
pHere is the funtion to revert in html/p
input type="button" onclick="revert();" value="Revert" / br/
input type="button" onclick="javascript:window.top.location.reload()" value="Refresh" /
/li
/div
div
p id="picpath"PhotoPath/p
img alt="ddd" id="pic0" width="200" height="200" src="/storage/sdcard0/webview_camera/testimg23.8156950140954181.jpg" /
/div
/body
/html
3、編寫Activity的布局activity_camera_webview.xml:
RelativeLayout xmlns:android=""
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CameraWebviewActivity"
Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera"
android:layout_alignParentTop="true"
/
WebView
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/bt"
/
/RelativeLayout
4.、編寫activity:
public class CameraWebviewActivity extends Activity {
private final static String TAG = "CameraWebviewActivity";
private Button bt;
private WebView wv;
public String fileFullName;//照相后的照片的全整路徑
private boolean fromTakePhoto; //是否是從攝像界面返回的webview
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_webview);
initViews();
}
private void initViews() {
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("----------------");
takePhoto( Math.random()*1000+1 + ".jpg");
}
});
wv = (WebView) findViewById(R.id.wv);
WebSettings setting = wv.getSettings();
setting.setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
wv.setWebChromeClient(new WebChromeClient(){
@Override//實現(xiàn)js中的alert彈窗在Activity中顯示
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(TAG, message);
result.confirm();
return true;
}
});
wv.loadUrl("");
final Handler mHandler = new Handler();
//webview增加javascript接口,監(jiān)聽html頁面中的js點擊事件
wv.addJavascriptInterface(new Object(){
public String clickOnAndroid() {//將被js調(diào)用
mHandler.post(new Runnable() {
public void run() {
fromTakePhoto = true;
//調(diào)用 啟用攝像頭的自定義方法
takePhoto("testimg" + Math.random()*1000+1 + ".jpg");
System.out.println("========fileFullName: " + fileFullName);
}
});
return fileFullName;
}
}, "demo");
}
/*
* 調(diào)用攝像頭的方法
*/
public void takePhoto(String filename) {
System.out.println("----start to take photo2 ----");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_MEDIA_TITLE, "TakePhoto");
//判斷是否有SD卡
String sdDir = null;
boolean isSDcardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(isSDcardExist) {
sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();
} else {
sdDir = Environment.getRootDirectory().getAbsolutePath();
}
//確定相片保存路徑
String targetDir = sdDir + "/" + "webview_camera";
File file = new File(targetDir);
if (!file.exists()) {
file.mkdirs();
}
fileFullName = targetDir + "/" + filename;
System.out.println("----taking photo fileFullName: " + fileFullName);
//初始化并調(diào)用攝像頭
intent.putExtra(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileFullName)));
startActivityForResult(intent, 1);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
* 重寫些方法,判斷是否從攝像Activity返回的webview activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("----requestCode: " + requestCode + "; resultCode " + resultCode + "; fileFullName: " + fileFullName);
if (fromTakePhoto requestCode ==1 resultCode ==-1) {
wv.loadUrl("javascript:wave2('" + fileFullName + "')");
} else {
wv.loadUrl("javascript:wave2('Please take your photo')");
}
fromTakePhoto = false;
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera_webview, menu);
return true;
}
}
1、在手機的設(shè)置里面去找相機的設(shè)置,
在照相的功能下也可以在選項里面找到更換攝像頭的選項,
一般都是按左鍵出來選單的。
2、在手機菜單里面勾選攝像頭選項,切換到你想要的攝像頭,然后把那個屏幕切換一下,豎屏或橫屏;
3、你若是在找不到就到手機鋪里面找售后維修或者在照片拍攝好后,在電腦上面把使用那個圖像管理器把圖片,點擊那個轉(zhuǎn)換頭,讓圖片正過來。
android 中實現(xiàn)網(wǎng)頁調(diào)用攝像頭功能方法·如下: 1/apk/res/android" xmlns:tools="schemas/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".CameraWebviewActivity" Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Camera" android:layout_alignParentTop="true" /WebView android:id="@+id/wv" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_below="@+id/bt" //RelativeLayout 4.、編寫activity: public class CameraWebviewActivity extends Activity {private final static String TAG = "CameraWebviewActivity";private Button bt; private WebView wv; public String fileFullName;//照相后的照片的全整路徑 private boolean fromTakePhoto; //是否是從攝像界面返回的webview@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_webview); initViews(); }private void initViews() {bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("----------------"); takePhoto( Math.random()*1000+1 + ".jpg"); } });wv = (WebView) findViewById(R.id.wv); WebSettings setting = wv.getSettings(); setting.setJavaScriptEnabled(true); wv.setWebViewClient(new WebViewClient(){ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); }@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url); }@Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); }});wv.setWebChromeClient(new WebChromeClient(){ @Override//實現(xiàn)js中的alert彈窗在Activity中顯示 public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d(TAG, message); result.confirm(); return true; } });wv.loadUrl(""); final Handler mHandler = new Handler(); //webview增加javascript接口,監(jiān)聽html頁面中的js點擊事件 wv.addJavascriptInterface(new Object(){ public String clickOnAndroid() {//將被js調(diào)用 mHandler.post(new Runnable() { public void run() { fromTakePhoto = true; //調(diào)用 啟用攝像頭的自定義方法 takePhoto("testimg" + Math.random()*1000+1 + ".jpg"); System.out.println("========fileFullName: " + fileFullName);} }); return fileFullName; } }, "demo"); }/* * 調(diào)用攝像頭的方法 */ public void takePhoto(String filename) { System.out.println("----start to take photo2 ----"); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_MEDIA_TITLE, "TakePhoto");//判斷是否有SD卡 String sdDir = null; boolean isSDcardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); if(isSDcardExist) { sdDir = Environment.getExternalStorageDirectory().getAbsolutePath(); } else { sdDir = Environment.getRootDirectory().getAbsolutePath(); } //確定相片保存路徑 String targetDir = sdDir + "/" + "webview_camera"; File file = new File(targetDir); if (!file.exists()) { file.mkdirs(); } fileFullName = targetDir + "/" + filename; System.out.println("----taking photo fileFullName: " + fileFullName); //初始化并調(diào)用攝像頭 intent.putExtra(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileFullName))); startActivityForResult(intent, 1); }/* * (non-Javadoc) * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) * 重寫些方法,判斷是否從攝像Activity返回的webview activity */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { System.out.println("----requestCode: " + requestCode + "; resultCode " + resultCode + "; fileFullName: " + fileFullName); if (fromTakePhoto requestCode ==1 resultCode ==-1) { wv.loadUrl("javascript:wave2('" + fileFullName + "')"); } else { wv.loadUrl("javascript:wave2('Please take your photo')"); } fromTakePhoto = false; super.onActivityResult(requestCode, resultCode, data); }@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.camera_webview, menu); return true; }}
這個權(quán)限的話通常都是軟件會自動獲取的,像有些軟件需要獲取手機的攝像頭調(diào)用權(quán)限,通常都會默認(rèn)配置的。
如果自己禁止了某些軟件調(diào)用攝像頭,這種情況的話,只需要在手機設(shè)置的權(quán)限管理里面自己設(shè)置一下離學(xué)校有攝像頭就可以了。
Android調(diào)用本機攝像頭后可以移動如下
調(diào)用本機攝像頭//設(shè)置返回碼:標(biāo)志照相機拍攝final int RESULT_CAMERA=100;//設(shè)置一個臨時路徑,保存所拍的照片String savefont = “/storage/sdcard0/”;// 獲取模板路徑File CameraFolder = new File(savefont, “CameraFile”);if (!CameraFolder.exists())//判斷文件夾是否存在,不存在則創(chuàng)建文件夾,已經(jīng)存在跳過{undefinedCameraFolder.mkdir()。
網(wǎng)頁題目:android攝像頭調(diào)用,android調(diào)用攝像頭讀取數(shù)據(jù)
本文地址:http://jinyejixie.com/article32/dsechsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、外貿(mào)建站、移動網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、電子商務(wù)、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)