gpt4 book ai didi

android - 使用视觉 API 在脸上拍摄可绘制/绘画的照片

转载 作者:可可西里 更新时间:2023-11-01 19:01:24 26 4
gpt4 key购买 nike

我在尝试什么?

我正在尝试在脸上拍摄可绘制/绘画的照片,但我无法在同一张照片上获得两者。

enter image description here

我尝试了什么?

我已经尝试使用 CameraSource.takePicture 但我只是得到了没有任何可绘制/绘画的脸。

mCameraSource.takePicture(shutterCallback, new CameraSource.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes) {
try {
String mainpath = getExternalStorageDirectory() + separator + "TestXyz" + separator + "images" + separator;
File basePath = new File(mainpath);
if (!basePath.exists())
Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success": "Failed");
String path = mainpath + "photo_" + getPhotoTime() + ".jpg";
File captureFile = new File(path);
captureFile.createNewFile();
if (!captureFile.exists())
Log.d("CAPTURE_FILE_PATH", captureFile.createNewFile() ? "Success": "Failed");
FileOutputStream stream = new FileOutputStream(captureFile);
stream.write(bytes);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});

我也尝试过使用:

mPreview.setDrawingCacheEnabled(true);
Bitmap drawingCache = mPreview.getDrawingCache();
try {
String mainpath = getExternalStorageDirectory() + separator + "TestXyz" + separator + "images" + separator;
File basePath = new File(mainpath);
if (!basePath.exists())
Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success": "Failed");
String path = mainpath + "photo_" + getPhotoTime() + ".jpg";
File captureFile = new File(path);
captureFile.createNewFile();
if (!captureFile.exists())
Log.d("CAPTURE_FILE_PATH", captureFile.createNewFile() ? "Success": "Failed");
FileOutputStream stream = new FileOutputStream(captureFile);
drawingCache.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}

在这种情况下,我只会得到我在脸上画的东西。在这里,mPreview 是 CameraSourcePreview .

刚刚添加了捕获按钮并在 this 中添加了上面的代码谷歌示例。

最佳答案

您非常接近实现您的需求:)

你有:

  1. 来自面部相机的图像(第一个代码 fragment )
  2. 来自眼睛覆盖 Canvas 的图像(第二个代码 fragment )

你需要什么:

  • 面部和眼睛叠加在上面的图像 - 合并后的图像。

如何合并?

要合并 2 个图像,只需使用 Canvas ,如下所示:

public Bitmap mergeBitmaps(Bitmap face, Bitmap overlay) {
// Create a new image with target size
int width = face.getWidth();
int height = face.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Rect faceRect = new Rect(0,0,width,height);
Rect overlayRect = new Rect(0,0,overlay.getWidth(),overlay.getHeight());

// Draw face and then overlay (Make sure rects are as needed)
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(face, faceRect, faceRect, null);
canvas.drawBitmap(overlay, overlayRect, faceRect, null);
return newBitmap
}

然后您可以像现在一样保存新图像。

完整代码如下:

mCameraSource.takePicture(shutterCallback, new 
CameraSource.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes) {
// Generate the Face Bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap face = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);

// Generate the Eyes Overlay Bitmap
mPreview.setDrawingCacheEnabled(true);
Bitmap overlay = mPreview.getDrawingCache();

// Generate the final merged image
Bitmap result = mergeBitmaps(face, overlay);

// Save result image to file
try {
String mainpath = getExternalStorageDirectory() + separator + "TestXyz" + separator + "images" + separator;
File basePath = new File(mainpath);
if (!basePath.exists())
Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success": "Failed");
String path = mainpath + "photo_" + getPhotoTime() + ".jpg";
File captureFile = new File(path);
captureFile.createNewFile();
if (!captureFile.exists())
Log.d("CAPTURE_FILE_PATH", captureFile.createNewFile() ? "Success": "Failed");
FileOutputStream stream = new FileOutputStream(captureFile);
result.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});

请注意,以上只是示例代码。您可能应该将合并和保存到文件的操作移至后台线程。

关于android - 使用视觉 API 在脸上拍摄可绘制/绘画的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45407964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com