【发布时间】:2015-01-15 00:22:55
【问题描述】:
我想实现自己的人脸检测/识别安卓应用。当相机找到某个人脸时,相机预览上会显示一个矩形(实时)。应用程序也有拍照的方法。但是,我不想保存整个图片,只保存矩形内的区域 - 人脸。当我将矩形坐标提供给 Bitmap.createBitmap 方法以裁剪我的图片时,裁剪照片的正确性取决于显示矩形的位置。当检测到的人脸出现在预览中间时,createBitmap 会对其进行裁剪,但如果它显示在显示屏的左侧或右侧,则不会。似乎我发送给 Bitmap.createBitmap 的坐标被转换了,但我找不到比率。有什么解决办法吗?
-
这是我的 onPictureTaken 方法:
@Override public void onPictureTaken(byte[] data, Camera camera) { File pictureFile = getOutputMediaFile(); if (pictureFile == null) { Log.d(TAG, "Error creating media file, check storage permissions: "); return; } Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length); RectF faceRect = mPreview.getFaceRect(); float x = faceRect.left; float y = faceRect.top; float w = faceRect.right - faceRect.left; float h = faceRect.bottom - faceRect.top; int intX = (int) x; int intY = (int) y; int intW = (int) w; int intH = (int) h; Bitmap croppedPicture = Bitmap.createBitmap(picture, intX, intY, intW, intH); ByteArrayOutputStream stream = new ByteArrayOutputStream(); croppedPicture.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] byteArrayFromPicture = stream.toByteArray(); try { FileOutputStream fos = new FileOutputStream(pictureFile); fos.write(byteArrayFromPicture); //fos.write(data); fos.close(); } catch (FileNotFoundException e) { Log.d(TAG, "File not found: " + e.getMessage()); } catch (IOException e) { Log.d(TAG, "Error accessing file: " + e.getMessage()); } }
这里是一些裁剪图片的例子,我没有足够的声誉来发布更多链接:
(抱歉把图片拍成图片,我懒得实现把矩形和照片一起保存)
【问题讨论】:
标签: android bitmap crop face-detection