導航:首頁 > 操作系統 > 調用android的camera

調用android的camera

發布時間:2022-05-31 18:32:44

㈠ 如何自定義開啟android攝像頭

開啟攝像頭的過程如下:
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 開發,調用相機的相關代碼

/**
* @Description 調用系統相機照相,獲取原圖像
* @param activity 就是你的mainactivity
* @param dir 拍照後保存在本地的圖片路徑
* @param filename 圖片的名稱
* @param cmd 返回的requestCode

* @return void
*/
public static boolean takePhoto(final Activity activity, final String dir,
final String filename, final int cmd) {
String filePath = dir + filename;

// final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Log.d("test", "MediaStore.ACTION_IMAGE_CAPTURE"
+ android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE == null ? "android.media.action.IMAGE_CAPTURE"
: android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final File cameraDir = new File(dir);
if (!cameraDir.exists()) {
// return false;
cameraDir.mkdirs();
}

final File file = new File(filePath);
final Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
try {
activity.startActivityForResult(intent, cmd);

} catch (final ActivityNotFoundException e) {
return false;
}
return true;
}

然後在你的onactivityforresult方法里寫:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case PHOTOGRAPH://拍照
if (resultCode == RESULT_OK) {
String url = FileHelper.Get_SDCardPath() + Constant.HHXH_IMGDIR
+ BitmapUtil.tempPhoto;//圖片路徑,和你前面調用相冊時設的路徑一樣
Bitmap bitmap = BitmapUtil
.extractThumbNail(url, 480, 320, true);//這里是根據本地路徑獲取圖片並壓縮的,這個我寫的代碼比較多就不上傳了
findviewbyid(R.id.imgPhoto).setImageBitmap(bitmap);

}
}
}

㈢ android camera啟動需要哪些resource

在菜單或按鈕的選擇操作中調用如下代碼,開啟系統自帶Camera APP,並傳遞一個拍照存儲的路徑給系統應用程序,具體如下:

imgPath = "/sdcard/test/img.jpg";

//必須確保文件夾路徑存在,否則拍照後無法完成回調

File vFile = new File(imgPath);

if(!vFile.exists())

{

File vDirPath = vFile.getParentFile(); //new File(vFile.getParent());

vDirPath.mkdirs();

}

Uri uri = Uri.fromFile(vFile);

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//

startActivityForResult(intent, SystemCapture);

上面使用的是startActivityForResult,所以最好需要重載void onActivityResult(int requestCode, int resultCode, Intent data)函數,不過因為當傳入文件路徑的的情況下,data返回參數是null值,只要resultCode為RESULT_OK,則上述代碼中/sdcard/test/img.jpg的圖片文件就是最新的照片文件。所以在這里只需給出如下簡單的代碼,將其顯示到ImageView中

if (resultCode == RESULT_OK)

{

iViewPic.setImageURI(Uri.fromFile(new File(imgPath)));

}

假設不傳參數MediaStore.EXTRA_OUTPUT的情況下,onActivityResult函數在resultCode為RESULT_OK的情況下,data返回的參數是經過實際拍攝照片經過縮放的圖像數據,可以通過類似如下方法來列印縮放圖像的尺寸

if (resultCode == RESULT_OK)

{

Bitmap bmp = (Bitmap)data.getExtras().get("data");

Log.d("Test", "bmp width:" + bmp.getWidth() + ", height:" + bmp.getHeight());

}

另外假如僅僅是調用系統照相機拍照,不關心拍照結果,則可以簡單使用如下代碼

Intent intent = new Intent(); //調用照相機

intent.setAction("android.media.action.STILL_IMAGE_CAMERA");

startActivity(intent);

備註:上面設置MediaStore.EXTRA_OUTPUT的方法,經過手機實測除了設定的路徑下有照片外,在手機存儲卡上也會保存一份照片,默認目錄為sdcard/dcim/camera下面,曾經嘗試著想如果每次返回可以取得sdcard/dcim/camera下面的路徑就好了,但是目前看來沒辦法直接獲得,可以藉助MediaStroe每次去查詢最後一條照片記錄,應該也是可行的。

㈣ Android開發怎麼調用攝像頭功能

1、現在Android智能手機的像素都會提供照相的功能,大部分的手機的攝像頭的像素都在1000萬以上的像素,有的甚至會更高。它們大多都會支持光學變焦、曝光以及快門等等。下面的程序Demo實例示範了使用Camera v2來進行拍照,當用戶按下拍照鍵時,該應用會自動對焦,當對焦成功時拍下照片。layout/activity_main.xml界面布局代碼如下:

3、接來了的MainActivity.java程序將會使用CameraManager來打開CameraDevice,並通過CameraDevice創建CameraCaptureSession,然後即可通過CameraCaptureSession進行預覽或拍照了。

㈤ android中怎麼啟動camera

Android中啟動camera相機,原理是直接調用系統的相機應用,只需要在Intent對象中傳入相應的參數即可。如下代碼:

在菜單或按鈕的選擇操作中調用如下代碼,開啟系統自帶CameraAPP,並傳遞一個拍照存儲的路徑給系統應用程序,具體如下:
imgPath="/sdcard/test/img.jpg";
//必須確保文件夾路徑存在,否則拍照後無法完成回調
FilevFile=newFile(imgPath);//新建一個File類,也就是照片保存的位置
if(!vFile.exists())//判斷該文件是否存在
{
FilevDirPath=vFile.getParentFile();//newFile(vFile.getParent());
vDirPath.mkdirs();
}
Uriuri=Uri.fromFile(vFile);//文件在android系統中uri地址
Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);//打開相機
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);//發送意圖,啟動相機
startActivityForResult(intent,SystemCapture);//啟動完成,返回值接收

㈥ Android開發(攝像頭的調用)(大神請進……)

打開攝像頭,要 start surfaceiew,當activity執行到onStop()時,要判斷,然後關閉surfaceiew。這樣應該就不會導致程序force close了。或者樓主您直接把force close的log信息打出來,有log才能准確的分析問題。

㈦ android應用怎樣調用setcameraflipstatus

調用系統Camera App實現拍照和攝像功能
不是專門的Camera應用,一般用到Camera的需求就是獲取照片或者視頻,比如微博分享、隨手記等,對於在Symbian系統上通過簡單地調用系統自帶的Camera APP來實現該功能是做不到的,但是Android系統強大的組件特性,使得應用開發者只需通過Intent就可以方便的打開系統自帶的Camera APP,並通過MediaStroe方便地獲取照片和視頻的文件路徑。具體我們還是用代碼來說話吧:

例1、 實現拍照
在菜單或按鈕的選擇操作中調用如下代碼,開啟系統自帶Camera APP,並傳遞一個拍照存儲的路徑給系統應用程序,具體如下:
imgPath = "/sdcard/test/img.jpg";
//必須確保文件夾路徑存在,否則拍照後無法完成回調
File vFile = new File(imgPath);
if(!vFile.exists())
{
File vDirPath = vFile.getParentFile(); //new File(vFile.getParent());
vDirPath.mkdirs();
}
Uri uri = Uri.fromFile(vFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//
startActivityForResult(intent, SystemCapture);

㈧ android中camera的hal模塊怎麼被調用

CameraService.cpp (frameworks\base\services\camera\libcameraservice)

中調用hw_get_mole

[cpp] view plain print?
void CameraService::onFirstRef()
{
BnCameraService::onFirstRef();

<span style="color: rgb(255, 0, 0);">if (hw_get_mole(CAMERA_HARDWARE_MODULE_ID,
(const hw_mole_t **)&mMole) < 0)</span> {
LOGE("Could not load camera HAL mole");
mNumberOfCameras = 0;
}
else {
mNumberOfCameras = mMole->get_number_of_cameras();
if (mNumberOfCameras > MAX_CAMERAS) {
LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
mNumberOfCameras, MAX_CAMERAS);
mNumberOfCameras = MAX_CAMERAS;
}
for (int i = 0; i < mNumberOfCameras; i++) {
setCameraFree(i);
}
}
}
void CameraService::onFirstRef()
{
BnCameraService::onFirstRef();

if (hw_get_mole(CAMERA_HARDWARE_MODULE_ID,
(const hw_mole_t **)&mMole) < 0) {
LOGE("Could not load camera HAL mole");
mNumberOfCameras = 0;
}
else {
mNumberOfCameras = mMole->get_number_of_cameras();
if (mNumberOfCameras > MAX_CAMERAS) {
LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
mNumberOfCameras, MAX_CAMERAS);
mNumberOfCameras = MAX_CAMERAS;
}
for (int i = 0; i < mNumberOfCameras; i++) {
setCameraFree(i);
}
}
}

看一下hw_get_mole是怎麼回事

[cpp] view plain print?
int hw_get_mole(const char *id, const struct hw_mole_t **mole)
{
return <span style="color: rgb(255, 0, 0);">hw_get_mole_by_class(id, NULL, mole);
</span>}
int hw_get_mole(const char *id, const struct hw_mole_t **mole)
{
return hw_get_mole_by_class(id, NULL, mole);
}

他只是一個封裝實際調用了[email protected] (hardware\libhardware)
好在不長,看看吧

[cpp] view plain print?
int hw_get_mole_by_class(const char *class_id, const char *inst,
const struct hw_mole_t **mole)
{
int status;
int i;
const struct hw_mole_t *hmi = NULL;
<span style="color: rgb(255, 0, 0);"> char prop[PATH_MAX];//幾個關鍵的數組
char path[PATH_MAX];//在下面起了重要
char name[PATH_MAX];//作用
</span>
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);//走這里

/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new of the library).
* We also assume that dlopen() is thread-safe.
*/

/* Loop through the configuration variants looking for a mole */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
if (i < HAL_VARIANT_KEYS_COUNT) {
if (property_get(variant_keys[i], prop, NULL) == 0)
<span style="color: rgb(255, 0, 0);">//在這里將prop的路徑得到,分別從
"ro.hardware[qcom]"
"ro.proct.board"[7x27],
"ro.board.platform"[msm7627a],
"ro.arch",
"ro.hw_platform"[QRD_SKU3-1100]
這幾個屬性文件中獲得硬體的信息
有些硬體信息的字元串會出現在編譯後生成的.so名字中</span>
{
continue;
}
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, prop);
if (access(path, R_OK) == 0) break;

snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, prop);<span style="color: rgb(255, 0, 0);">//走這里,在這里得到/system/lib/hw/camera.msm7627a.so
這樣一個路徑,這個庫里有QualcommCamera.cpp,這是
camera模塊HAL代碼開始的地方</span>
if (access(path, R_OK) == 0) break;
} else {
snprintf(path, sizeof(path), "%s/%s.default.so",
HAL_LIBRARY_PATH1, name);
if (access(path, R_OK) == 0) break;
}
}

status = -ENOENT;
if (i < HAL_VARIANT_KEYS_COUNT+1) {
/* load the mole, if this fails, we're doomed, and we should not try
* to load a different variant. */
status = load(class_id, path, mole);<span style="color: rgb(255, 0, 0);">//這里關鍵,函數的三個參數可以串聯成一句話:
到path(/system/lib/hw/camera.msm7627a.so)這個路徑下找到一個id(camera)匹配的mole</span>
}

return status;
}
int hw_get_mole_by_class(const char *class_id, const char *inst,
const struct hw_mole_t **mole)
{
int status;
int i;
const struct hw_mole_t *hmi = NULL;
char prop[PATH_MAX];//幾個關鍵的數組
char path[PATH_MAX];//在下面起了重要
char name[PATH_MAX];//作用

if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);//走這里

/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new of the library).
* We also assume that dlopen() is thread-safe.
*/

/* Loop through the configuration variants looking for a mole */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
if (i < HAL_VARIANT_KEYS_COUNT) {
if (property_get(variant_keys[i], prop, NULL) == 0)
//在這里將prop的路徑得到,分別從
"ro.hardware[qcom]"
"ro.proct.board"[7x27],
"ro.board.platform"[msm7627a],
"ro.arch",
"ro.hw_platform"[QRD_SKU3-1100]
這幾個屬性文件中獲得硬體的信息
有些硬體信息的字元串會出現在編譯後生成的.so名字中
{
continue;
}
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, prop);
if (access(path, R_OK) == 0) break;

snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, prop);//走這里,在這里得到/system/lib/hw/camera.msm7627a.so
這樣一個路徑,這個庫里有QualcommCamera.cpp,這是
camera模塊HAL代碼開始的地方
if (access(path, R_OK) == 0) break;
} else {
snprintf(path, sizeof(path), "%s/%s.default.so",
HAL_LIBRARY_PATH1, name);
if (access(path, R_OK) == 0) break;
}
}

status = -ENOENT;
if (i < HAL_VARIANT_KEYS_COUNT+1) {
/* load the mole, if this fails, we're doomed, and we should not try
* to load a different variant. */
status = load(class_id, path, mole);//這里關鍵,函數的三個參數可以串聯成一句話:
到path(/system/lib/hw/camera.msm7627a.so)這個路徑下找到一個id(camera)匹配的mole
}

return status;
}

再來看看load這個函數@hardware.c (hardware\libhardware)

[cpp] view plain print?
static int load(const char *id,
const char *path,
const struct hw_mole_t **pHmi)
{
int status;
void *handle;
struct hw_mole_t *hmi;

/*
* load the symbols resolving undefined symbols before
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
* RTLD_NOW the external symbols will not be global
*/
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
char const *err_str = dlerror();
LOGE("load: mole=%s\n%s", path, err_str?err_str:"unknown");
status = -EINVAL;
goto done;
}

<span style="color: rgb(255, 0, 0);"> /* Get the address of the struct hal_mole_info. */
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_mole_t *)dlsym(handle, sym);
</span> if (hmi == NULL) {
LOGE("load: couldn't find symbol %s", sym);
status = -EINVAL;
goto done;
}

/* Check that the id matches */
if (strcmp(id, hmi->id) != 0) {
LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
status = -EINVAL;
goto done;
}

hmi->dso = handle;

/* success */
status = 0;

done:
if (status != 0) {
hmi = NULL;
if (handle != NULL) {
dlclose(handle);
handle = NULL;
}
} else {
LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
id, path, *pHmi, handle);
}

*pHmi = hmi;

return status;
}
static int load(const char *id,
const char *path,
const struct hw_mole_t **pHmi)
{
int status;
void *handle;
struct hw_mole_t *hmi;

/*
* load the symbols resolving undefined symbols before
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
* RTLD_NOW the external symbols will not be global
*/
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
char const *err_str = dlerror();
LOGE("load: mole=%s\n%s", path, err_str?err_str:"unknown");
status = -EINVAL;
goto done;
}

/* Get the address of the struct hal_mole_info. */
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_mole_t *)dlsym(handle, sym);
if (hmi == NULL) {
LOGE("load: couldn't find symbol %s", sym);
status = -EINVAL;
goto done;
}

/* Check that the id matches */
if (strcmp(id, hmi->id) != 0) {
LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
status = -EINVAL;
goto done;
}

hmi->dso = handle;

/* success */
status = 0;

done:
if (status != 0) {
hmi = NULL;
if (handle != NULL) {
dlclose(handle);
handle = NULL;
}
} else {
LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
id, path, *pHmi, handle);
}

*pHmi = hmi;

return status;
}

在打開的.so(camera.msm7627a.so)中查找HMI符號的地址,並保存在hmi中。至此,.so中的hw_mole_t已經被成功獲取,從而可以根
據它獲取別的相關介面。
1)HAL通過hw_get_mole函數獲取hw_mole_t
2)HAL通過hw_mole_t->methods->open獲取hw_device_t指針,並在此open函數中初始化hw_device_t的包裝結構中的
函數及hw_device_t中的close函數,如gralloc_device_open。
3)三個重要的數據結構:
a) struct hw_device_t: 表示硬體設備,存儲了各種硬體設備的公共屬性和方法
b)struct hw_mole_t: 可用hw_get_mole進行載入的mole
c)struct hw_mole_methods_t: 用於定義操作設備的方法,其中只定義了一個打開設備的方法open.

㈨ 如何直接調用安卓Android的系統相機的前置

一種方式是採用MediaStore,調用系統原生的相機。

㈩ android 中實現網頁調用攝像頭功能怎麼實現

頁面上做一個按鈕,如打開攝像頭,它做的事情就是將頁面鏈接到一個特殊的串,如<a href=opencapture打開攝像頭</a使用安卓的webview訪問網頁頁面,同時重寫shouldOverrideUrlLoading捕獲url,當判斷url為opencapture時,調用系統攝像頭

閱讀全文

與調用android的camera相關的資料

熱點內容
不能修改的pdf 瀏覽:736
同城公眾源碼 瀏覽:474
一個伺服器2個埠怎麼映射 瀏覽:282
java字元串ascii碼 瀏覽:61
台灣雲伺服器怎麼租伺服器 瀏覽:461
旅遊手機網站源碼 瀏覽:316
android關聯表 瀏覽:929
安卓導航無聲音怎麼維修 瀏覽:322
app怎麼裝視頻 瀏覽:423
安卓系統下的軟體怎麼移到桌面 瀏覽:80
windows拷貝到linux 瀏覽:755
mdr軟體解壓和別人不一樣 瀏覽:888
單片機串列通信有什麼好處 瀏覽:324
游戲開發程序員書籍 瀏覽:848
pdf中圖片修改 瀏覽:275
匯編編譯後 瀏覽:480
php和java整合 瀏覽:835
js中執行php代碼 瀏覽:447
國產單片機廠商 瀏覽:62
蘋果手機怎麼設置不更新app軟體 瀏覽:289