重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Android调用本机摄像头后可以移动如下
10多年的准格尔网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整准格尔建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“准格尔网站设计”,“准格尔网站推广”以来,每个客户项目都认真落实执行。
调用本机摄像头//设置返回码:标志照相机拍摄final int RESULT_CAMERA=100;//设置一个临时路径,保存所拍的照片String savefont = “/storage/sdcard0/”;// 获取模板路径File CameraFolder = new File(savefont, “CameraFile”);if (!CameraFolder.exists())//判断文件夹是否存在,不存在则创建文件夹,已经存在跳过{undefinedCameraFolder.mkdir()。
这个权限的话通常都是软件会自动获取的,像有些软件需要获取手机的摄像头调用权限,通常都会默认配置的。
如果自己禁止了某些软件调用摄像头,这种情况的话,只需要在手机设置的权限管理里面自己设置一下离学校有摄像头就可以了。
开启摄像头的过程如下:
Android提供了Camera来控制拍照,步骤如下:
(1)调用Camera的open()方法打开相机。
(2)调用Camera的getParameters()获取拍照参数,该方法返回一个Cmera.Parameters对象。
(3)调用Camera.Parameters对象对照相的参数进行设置。
(4)调用Camera的setParameters(),并将Camera.Parameters对象作为参数传入,这样就可以对拍照进行参数控制,Android2.3.3以后不用设置。
(5)调用Camerade的startPreview()的方法开始预览取景,在之前需要调用Camera的setPreviewDisplay(SurfaceHolder holder)设置使用哪个SurfaceView来显示取得的图片。
(6)调用Camera的takePicture()方法进行拍照。
(7)程序结束时,要调用Camera的stopPreview()方法停止预览,并且通过Camera.release()来释放资源。
需要赋予Camera的权限:
1
2
3
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/
uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/
uses-permission android:name="android.permission.CAMERA"/
下面上代码:
android 中实现网页调用摄像头功能方法·如下:
1. 首先在Manifest.xml中添加如下权限:
!-- 访问网络权限 --
uses-permission android:name="android.permission.INTERNET"/
!-- 调用摄像头的权限 --
uses-permission android:name="android.permission.CAMERA"/
!-- 文件系统权限 --
uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/
!-- SD卡写权限 --
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中调用js方法
function wave() {
alert('cc2');
document.getElementById("droid").innerHTML ="android_waving.png";
}
//android中调用js方法2,并更id为"droid"的span标签的值及新刚拍的照片显示到"pic0"里
function wave2(name) {
alert(name);
document.getElementById("droid").innerHTML = "Photopath: " + name;
document.getElementById("pic0").src=name;
}
//html的点击事件,并通过"window.demo.clickOnAndroid"触发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"触发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//实现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接口,监听html页面中的js点击事件
wv.addJavascriptInterface(new Object(){
public String clickOnAndroid() {//将被js调用
mHandler.post(new Runnable() {
public void run() {
fromTakePhoto = true;
//调用 启用摄像头的自定义方法
takePhoto("testimg" + Math.random()*1000+1 + ".jpg");
System.out.println("========fileFullName: " + fileFullName);
}
});
return fileFullName;
}
}, "demo");
}
/*
* 调用摄像头的方法
*/
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);
//初始化并调用摄像头
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;
}
}
android 中实现网页调用摄像头功能方法·如下: 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//实现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接口,监听html页面中的js点击事件 wv.addJavascriptInterface(new Object(){ public String clickOnAndroid() {//将被js调用 mHandler.post(new Runnable() { public void run() { fromTakePhoto = true; //调用 启用摄像头的自定义方法 takePhoto("testimg" + Math.random()*1000+1 + ".jpg"); System.out.println("========fileFullName: " + fileFullName);} }); return fileFullName; } }, "demo"); }/* * 调用摄像头的方法 */ 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); //初始化并调用摄像头 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、在手机的设置里面去找相机的设置,
在照相的功能下也可以在选项里面找到更换摄像头的选项,
一般都是按左键出来选单的。
2、在手机菜单里面勾选摄像头选项,切换到你想要的摄像头,然后把那个屏幕切换一下,竖屏或横屏;
3、你若是在找不到就到手机铺里面找售后维修或者在照片拍摄好后,在电脑上面把使用那个图像管理器把图片,点击那个转换头,让图片正过来。