本文介绍了如何将Java代码转换为Android兼容代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 import static com.googlecode.javacv.cpp.opencv_core.CV_AA;import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;import static com.googlecode.javacv.cpp.opencv_core.cvLoad;import static com.googlecode.javacv.cpp.opencv_core.cvPoint;import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;import static com.googlecode.javacv.cpp.opencv_core.cvSetImageROI;import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;import java.util.Random;import com.googlecode.javacv.CanvasFrame;import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;import com.googlecode.javacv.cpp.opencv_core.CvRect;import com.googlecode.javacv.cpp.opencv_core.CvScalar;import com.googlecode.javacv.cpp.opencv_core.CvSeq;import com.googlecode.javacv.cpp.opencv_core.IplImage;import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;public class FaceDetection {// The cascade definition to be used for detection.private static final String CASCADE_FILE = "C:\\OpenCV2.3\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";public static void main(String arg[]) throws Exception {float randomNumber = 0;Random random = new Random();randomNumber = random.nextFloat();// Load the original image.IplImage originalImage = cvLoadImage("E:\\4 th Year 1Semester\\Project\\JavaCVApp\\Steve-Jobs-iphone_320.jpg", 1);// create image window named "My Image"final CanvasFrame canvas = new CanvasFrame("My Image");// request closing of the application when the image window is closedcanvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);// We need a grayscale image in order to do the recognition, so we// create a new image of the same size as the original one.IplImage grayImage = IplImage.create(originalImage.width(),originalImage.height(), IPL_DEPTH_8U, 1);// We convert the original image to grayscale.cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);CvMemStorage storage = CvMemStorage.create();// We instantiate a classifier cascade to be used for detection, using// the cascade definition.CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));// We detect the faces.CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 3,0);// We iterate over the discovered faces and draw yellow rectangles// around them.for (int i = 0; i < faces.total(); i++) {CvRect r = new CvRect(cvGetSeqElem(faces, i));cvRectangle(originalImage, cvPoint(r.x(), r.y()),cvPoint(r.x() + r.width(), r.y() + r.height()),CvScalar.YELLOW, 1, CV_AA, 0);// show image on windowcanvas.showImage(originalImage);}for (int i = 0; i < faces.total(); i++) {CvRect r = new CvRect(cvGetSeqElem(faces, i));cvRectangle(originalImage, cvPoint(r.x(), r.y()),cvPoint(r.x() + r.width(), r.y() + r.height()),CvScalar.YELLOW, 1, CV_AA, 0);cvSetImageROI(originalImage, r);// Save the image to a new file.cvSaveImage("E:\\4 th Year 1Semester\\Project\\JavaCVApp\\src\\"+ randomNumber + i + ".jpg", originalImage);}System.out.println("nu=" + faces.total());}} 推荐答案 这篇关于如何将Java代码转换为Android兼容代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 16:36