• 回首頁

    技術(code)參考

    歡迎來了解十個嬪妃一個美的專題介紹

    多重選取圖片:

    使用imagecursor去記憶體拿照片,還要對應顯示圖片位置。

    Cursor imagecursor = managedQuery(

    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy + "DESC");

    galleryImageUrls = new ArrayList();

    for (int i = 0; i < imagecursor.getCount(); i++) {

    imagecursor.moveToPosition(i);

    int dataColumnIndex =imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); galleryImageUrls.add(imagecursor.getString(dataColumnIndex)); }

    人臉辨識:

    使用android內建face detection功能,及可計算出人臉座標、人眼中點座標和偵測到的人臉數。但是手機自動存取圖片時是存放成橫的格式,因此需要把照片轉90度再做人臉辨識。

    m.setRotate(-90);

    bm = Bitmap.createBitmap(bm, 0, 0, imageWidth, imageHeight, m, true);

    Bitmap tempbitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.RGB_565);

    myFace = new FaceDetector.Face[numberOfFace];

    myFaceDetect = new FaceDetector(bm.getWidth(), bm.getHeight(), numberOfFace);

    numberOfFaceDetected = myFaceDetect.findFaces(bm, myFace);

    binarymap = bm.copy(Bitmap.Config.ARGB_8888, true);

    眼高辨識:

    做眼高辨識時要先將圖片二值化,用偵測出的人眼座標為基準,往上(下)移動尋找,遇到連續兩個白點即便是為眼睛上緣(下)。

    for (int i = 0; i < bm.getWidth(); i++) {

    for (int j = 0; j < bm.getHeight(); j++) {

    int col = binarymap.getPixel(i, j);

    int alpha = col & 0xFF000000;

    int red = (col & 0x00FF0000) >> 16;

    int green = (col & 0x0000FF00) >> 8;

    int blue = (col & 0x000000FF);

    int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);

    if (gray <= 100)

    gray = 0;

    else

    gray = 255;

    int newColor = alpha | (gray << 16) | (gray << 8) | gray;

    binarymap.setPixel(i, j, newColor);

    }

    }

    for (int a = (int) y; a < (int) (y + (myEyesDistance / 2.0)); a++) {

    int col = binarymap.getPixel((int) (x - myEyesDistance / 2), a);

    int red = (col & 0x00FF0000) >> 16;

    int green = (col & 0x0000FF00) >> 8;

    int blue = (col & 0x000000FF);

    int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);

    if (gray == 255)

    counter1++;

    if (counter1 > 2) {

    up[k] = a;

    canvas.drawLine(x - myEyesDistance / 2, up[k], x + myEyesDistance / 2, up[k], myPaint1);

    counter1 = 0;

    a = (int) (y + (myEyesDistance / 2));

    }

    }

    for (int a = (int) y; a > (int) (y - (myEyesDistance / 2)); a--) {

    int col = binarymap.getPixel((int) (x - myEyesDistance / 2), a);

    int red = (col & 0x00FF0000) >> 16;

    int green = (col & 0x0000FF00) >> 8;

    int blue = (col & 0x000000FF);

    int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);

    if (gray == 255) {

    counter++;

    }

    if (counter > 2) {

    down[k] = a;

    canvas.drawLine(x - myEyesDistance / 2, down[k], x + myEyesDistance / 2, down[k], myPaint1);

    counter = 0;

    a = (int) (y - (myEyesDistance / 2));

    }

    }

    height[k] = up[k] - down[k]; }

    比較和裁減:

    比較:比較個人在十連拍中眼睛睜最大的一張照片。

    if (max < eye_h.get(i)) {

    max_i = i;

    max = eye_h.get(i);

    }

    裁剪:訂定一張照片為底圖,將每個人的最佳臉貼上合成,使用getpixel一個一個pixel慢慢合成會造成速度過慢,因為太多資料需要處理,因此改用getpixels直接剪下一塊資料在貼上,可以加快速度,在十秒內可完成APP的計算。

    BmCrop.getPixels(pixels, 0, BmCrop.getWidth(),xPixel,yPixel, width, heigh); mBitmap.setPixels(pixels, 0, mBitmap.getWidth(),xPixel,yPixel, width, heigh);

  • 回首頁