android - Canvas 缩放保留像素大小

标签 android image-processing scaling

我正在尝试对相机预览进行一些图像处理,但速度太慢,我在采样时间图像下进行了较小规模的处理。然后我将处理过的(阈值化的)图像覆盖在预览上。我的问题是,当我缩放较小的图像以适合相机预览时,像素保持相同大小...由于缩放,我希望看到一些大像素,但我不能。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/svPreview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <SurfaceView
        android:id="@+id/svOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />
</RelativeLayout>

获取相机预览图像字节并进行阈值处理的线程:

class PathThread extends Thread implements PreviewCallback
{
    private final int width, height;
    private final SurfaceHolder holder;
    private Canvas overlayCanvas;
    private int samples = 8;

    final private float scale;
    final private Paint red, blue, yellow;

    public PathThread(int canvasWidth, int canvasHeight, SurfaceView canvasView)
    {
        // This is how much smaller image I want
        width = canvasWidth / samples;
        height = canvasHeight / samples;
        holder = canvasView.getHolder();

        // Scale to fit the camera preview SurfaceView
        scale = (float) canvasView.getWidth() / height;

        // No smoothing when scaling please
        yellow = new Paint();
        yellow.setColor(Color.parseColor("#ffbb33"));
        yellow.setAntiAlias(false);  
        yellow.setDither(false);  
        yellow.setFilterBitmap(false);
    }

    public void onPreviewFrame(byte[] data, Camera camera)
    {
        overlayCanvas = holder.lockCanvas();
        overlayCanvas.drawColor(0, Mode.CLEAR);
        overlayCanvas.scale(scale, scale);

        // Do the image processing and make a [samples] times smaller image
        // Boring, pseudo-optimized, per-pixel thresholding process

        holder.unlockCanvasAndPost(overlayCanvas);
    }
}

这是结果:

Scaled overlay pixels are of original size

我知道这有点偏差,但这是我遇到的最少的问题。

最佳答案

解决方案:

经过 2 天多的搜索,它很简单,但不是很容易找到/理解:

对于我绘制的 SurfaceView 的持有者,我不得不调用它:

canvasHolder.setFixedSize(width, height);

我尝试了不同的值并找到了我需要的。这使得大像素

关于android - Canvas 缩放保留像素大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12342291/

相关文章:

android - 执行脚本IntrinsicYuvToRgb

安卓 : How to store address into native Contact app from our app

java - 如何更改照片?

image - 为什么 imagesc 会改变颜色图 (MATLAB)

mysql - 在MySQL中查询分片数据

php - 获取单个大记录,还是获取多个小记录?

android - 使用 dp 会处理物理屏幕尺寸吗?

android - 启发我的 Play 商店帐户

python - 使用opencv和python计算相机到物体的距离

c++ - 如何使用 C++ 在 openCV 中显示图像元数据?