android - 拍摄照片并应用效果(缩放、旋转、灰色、用手指绘制)

标签 android android-camera2

我正在尝试拍摄照片,然后允许用户添加一些效果、绘图、将其他 Assets 拖到图像、添加文本等。就像 snapchat 相机。

我已经关注了 Camera 2 API sample .代码的主要部分位于 Camera2BasicFragment.java

我已经实现了捕获和预览图像,但是示例将图像设置在 TextureView 中,但我不知道如何继续操作图像。

我不知道我是否应该使用 TextureViewCanvasSurfaceView

我想要的最终图像结果示例:

example

提前致谢。

最佳答案

拍摄您的照片,保存您的图像。然后在 ImageView 中,您可以使用 BitmapColorMatrix 应用您想要的所有类型的效果。 这里我给出一个小样本来制作图像灰度

    public void toGrayscale() {
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(0);

        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
        imageView.setColorFilter(filter);
    }

应用效果后,只需将可绘制对象保存到图像文件中,如下所示:

public void saveDrawable(ImageView imageView) throws FileNotFoundException {
    Bitmap bitmap = getBitmapFromImageView(imageView);
    OutputStream fOut = null;
    try {
        fOut = new FileOutputStream(absolutePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 95, fOut);
    } finally {
        if (fOut != null) {
            try {
                fOut.close();
            } catch (IOException e) {
                //report error
            }
        }

    }
}

@NonNull
private Bitmap getBitmapFromImageView(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Rect bounds = drawable.getBounds();
    Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.draw(canvas);
    return bitmap;
}

有一个很好的图书馆可以帮助你https://github.com/chrisbanes/PhotoView


用手指画出这个问题how to draw line on imageview along with finger in android应该对你有帮助

希望对您有所帮助!!

关于android - 拍摄照片并应用效果(缩放、旋转、灰色、用手指绘制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35604954/

相关文章:

android - 向应用程序发送通知。 (空气)

Android 触摸和滚动 ListView

android - 致命异常 : java. lang.IllegalArgumentException : Receiver not registered: android. hardware.camera2.CameraManager

android - 全屏预览camera2Basic示例工程

android - 使用Android的相机拍摄灰度照片2

android - surfaceCreated() 从未被调用

android - 为什么更新我的 Android Studio 后会显示错误类型不匹配?

android - 在捕获图片 Android Camera 2 Api 时保持手电筒模式有问题吗?

Android 相机视频为空白,带有白线

android为什么这个按钮不可点击