2012-09-09 116 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); 

我嘗試了不同的值,並找到了我需要的東西。這使得大像素