English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android 인식 Demo 세로 YUV 방향 조정 및 이미지 저장(공유)

이 블로그에는 세 가지 일반적인 메서드가 포함되어 있으며, Android 버전 인脸 인식 Demo에서 가로 화면 사용 시 yuv 데이터를 넣을 때 인식되지 않는 경우에 사용됩니다.

1를 시도할 수 있습니다.90° 또는270°를 회전하고 인식 SDK에 넣습니다.

2이후에도 회전 방향이 인식되지 않으면 saveImg()를 시도하여 로컬에 이미지가 요구 사항에 맞는지 확인할 수 있습니다.

 /**
  * 비디오가 시계 방향으로 회전90
  * 이 메서드는 가로 화면에서만 사용됩니다
  * */
 public static byte[] rotateYUV420Degree90(byte[] data, int imageWidth,
          int imageHeight) {
  byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2);
  int i = 0;
  for (int x = 0; x < imageWidth; x++) {
   for (int y = imageHeight - 1; y >= 0; y--) {
    yuv[i] = data[y * imageWidth + x)];
    i++;
   }
  }
  i = imageWidth * imageHeight * 3 / 2 - 1;
  for (int x = imageWidth - 1; x > 0; x = x - 2) {
   for (int y = 0; y < imageHeight / 2; y++) {
    yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x)];
    i--;
    yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth)
      + (x - 1);
    i--;
   }
  }
  return yuv;
 }
 public static byte[] YUV420spRotate270(byte[] src, int width, int height) {
  int count = 0;
  int uvHeight = height >> 1;
  int imgSize = width * height;
  byte[] des = new byte[imgSize * 3 >> 1);
  //copy y
  for (int j = width - 1; j >= 0; j--) {
   for (int i = 0; i < height; i++) {
    des[count++] = src[width * i + j];
   }
  }
  //u,v
  for (int j = width - 1; j > 0; j -= 2) {
   for (int i = 0; i < uvHeight; i++) {
    des[count++] = src[imgSize + width * i + j - 1);
    des[count++] = src[imgSize + width * i + j];
   }
  }
  return des;
 }
 private int i = 1;
 private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/0Face/";
 private Calendar now = new GregorianCalendar();
 private SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
 private String fileName = simpleDate.format(now.getTime());
 /**
  * @param data yuv 이미지 데이터
  * @param width 
  * @param height
  */
 public void saveImg(byte[] data, int width, int height) {
  File dir = new File(path);
  if (!dir.exists()) dir.mkdirs();
  File f = new File(path + (fileName + "-" + i++) + ".jpg");
  FileOutputStream fOut = null;
  try {
   //yuv를 bitmap으로 변환
   YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null);
   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   image.compressToJpeg(new Rect(0, 0, width, height), 80, stream);
   Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
   //로컬에 비트맵 저장
   fOut = new FileOutputStream(f);
   bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
   fOut.flush();
   fOut.close();
   bmp.recycle();
   stream.close();
  } catch (Exception ex) {
   Log.e("Sys", "오류:" + ex.getMessage());
  }
 }

이번에 공유한 안드로이드 인脸 인식 데모가 직사각형 YUV 방향 조정과 이미지 저장(공유)에 대해서는 이제 모든 내용을 공유했습니다. 이 가이드가 도움이 되길 바라며, 많은 지지와 응원을 부탁드립니다.

명시: 본문은 인터넷에서 수집된 내용으로, 저작권자에게 속하며, 인터넷 사용자가 자발적으로 기여하고 업로드한 내용입니다. 이 웹사이트는 소유권을 가지지 않으며, 인공적으로 편집되지 않았으며, 관련 법적 책임도 부담하지 않습니다. 저작권 침해가 의심되는 내용이 발견되면, notice#w로 이메일을 보내 주시기 바랍니다.3codebox.com(보내는 이메일에서 #을 @으로 변경하여 신고하시고, 관련 증거를 제공하시면, 실제로 확인되면 해당 사이트는 즉시 저작권 침해 내용을 제거합니다。

좋아하는 것