android攝像頭opencv
『壹』 opencv for android 如何實現後台啟動攝像頭,不顯示預覽界面
現在Android智能手機的像素都會提供照相的功能,大部分的手機的攝像頭的像素都在1000萬以上的像素,有的甚至會更高。它們大多都會支持光學變焦、曝光以及快門等等。
下面的程序Demo實例示範了使用Camera v2來進行拍照,當用戶按下拍照鍵時,該應用會自動對焦,當對焦成功時拍下照片。
『貳』 opencv for android裡面的用到攝像頭的幾個官方例子,真機測試時攝像頭都打不開,黑屏不報錯
OpenCV
整個項目的結構圖:
編寫DetectFaceDemo.java,代碼如下:
[java] view
plainprint?
package com.njupt.zhb.test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路徑會多列印一個『/』,因此總是出現如下錯誤
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我們將第一個字元去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
package com.njupt.zhb.test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路徑會多列印一個『/』,因此總是出現如下錯誤
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我們將第一個字元去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
3.編寫測試類:
[java] view
plainprint?
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//運行結果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//運行結果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
『叄』 opencv for android中使用照相機前置攝像頭是顯示是倒置的
flip、transpose都在頭文件core.hpp,函數原型:
CV_EXPORTS_W void flip(InputArray src, OutputArray dst, int flipCode);
參數:
CV_EXPORTS_W void transpose(InputArray src, OutputArray dst);
transpose應該是求矩陣的轉置,估計你得用flip函數
『肆』 Android開發怎麼調用攝像頭功能
1、現在Android智能手機的像素都會提圓咐前供照相的功能,大部分的手機的攝橘清像頭的像素都在1000萬以上的像素,有的甚至會更高。它們大多簡鏈都會支持光學變焦、曝光以及快門等等。下面的程序Demo實例示範了使用Camera v2來進行拍照,當用戶按下拍照鍵時,該應用會自動對焦,當對焦成功時拍下照片。layout/activity_main.xml界面布局代碼如下:
3、接來了的MainActivity.java程序將會使用CameraManager來打開CameraDevice,並通過CameraDevice創建CameraCaptureSession,然後即可通過CameraCaptureSession進行預覽或拍照了。
『伍』 如何在Android中使用OpenCV
1、下載
進入官網下載OpenCV4Android並解壓。
其中,sdk目錄即是我們開發opencv所需要的類庫;samples目錄中存放著若干opencv應用示例(包括人臉檢測等),可為我們進行android下的opencv開發提供參考;doc目錄為opencv類庫的使用說明及api文檔等;而apk目錄則存放著對應於各內核版本的OpenCV_2.4.3.2_Manager_2.4應用安裝包。此應用用來管理手機設備中的opencv類庫,在運行opencv應用之前,必須確保手機中已經安裝了OpenCV_2.4.3.2_Manager_2.4_*.apk,否則opencv應用將會因為無法載入opencv類庫而無法運行(下篇文章會介紹不提前安裝openCV Manager,即可運行openCV的方法)。
2、將OpenCV引入Android Studio
在Android Studio中選擇File->Import Mole,找到OpenCV解壓的路徑,選擇sdk/java文件夾。
3、更新build.gradle信息
在Android Studio中的左上角選擇Project視圖,在oepnCVLibrary2411文件夾里,打開build.gradle(有很多重名的文件,一定找對openCV庫文件下的),修改文件中的1)compileSdkVersion 2)buildToolsVersion 3) minSdkVersion 4)targetSdkVersion,將其內容與app文件夾下的build.gradle中信息相一致。點擊上方提示的黃色提示框內的Try Again進行更新。
4、添加Mole Dependency
右鍵app文件夾,選擇Open Mole Settings,在app mole的Dependencies一欄中,點擊右上角的綠色加號,將openCVLibrary2411添加進去,點擊確定。
5、復制libs文件夾到項目中
在OpenCV的解壓包中,將sdk-->native-->libs文件夾復制,粘貼在Project視圖下app-->src-->main目錄下,並將其重命名為jniLibs。
自此,OpenCV的環境就配置好了。可以將OpenCV-android-sdk-->samples-->tutorial-1-camerapreview中的layout文件,java文件,放入工程中,修改AndroidManifest.xml,添加使用攝像機的許可權,即可測試是否成功了。