2012-09-09 219 views
1

我想在相机预览上做一些图像处理,但速度太慢,我在采样时间图像下,并在较小规模上进行处理。然后,我将处理后的(阈值)图像叠加在预览上。我的问题是,当我缩放较小的图像以适应相机预览时,像素保持相同的大小...我想看到一些大的像素由于缩放,但我不能。画布缩放保留像素尺寸

这里的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

我知道这有点偏移,但就是这样我的问题最少。

回答

1

解决方法:

后2个搜索这天是相当简单的,但不是很容易找到/理解:

与在我画的SurfaceView的持有人,我不得不称之为:

canvasHolder.setFixedSize(width, height); 

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