2016-11-25 69 views
0

我讓用戶選擇圖像分辨率,例如4096x3024px,然後從該流中獲取可變位圖,然後將其發送到surfaceview並創建另一個畫布,用於從屏幕大小縮放到位圖大小。Android從相機獲取位圖,將畫布縮放到位圖大小並保存位圖

當我縮放這張畫布時,圖像沒有正確放大。我試圖創建一個矩陣從中心的樞軸放大,但仍然沒有結果。

這其中i從相機預覽得到位圖和畫到用於縮放和保存

public Bitmap mergeImageAndCanvas(Bitmap bitmap) { 

    // DRAW TO Canvas created in constructor 
    canvasImage.setBitmap(bitmap); 
    render(canvasImage, true); 
    canvasImage.setBitmap(null); 
    System.out.println("Canvas " + canvas.getWidth() + ", height " + canvas.getHeight()); 

    return bitmap; 
} 

在這裏,我高檔和嘗試中心的圖像,以保持文本和線條我畫

的確切位置帆布
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    // Toast.makeText(getContext(), "Surfaceview surfaceChanged() width " + 
    // width + ", height" + height, 
    // Toast.LENGTH_LONG).show(); 
    bitmapWitdh = photoHandler.photoWidth; 
    bitmapHeight = photoHandler.photoHeight; 
    canvasWidth = width; 
    ratioX = (float) canvasWidth/1280; 
    canvasHeight = height; 
    scaleX = ((float) bitmapWitdh)/(float) canvasWidth; 
    scaleY = ((float) bitmapHeight)/(float) canvasHeight; 
    float middleX = bitmapWitdh/2.0f; 
    float middleY = bitmapHeight/2.0f; 
    scaleMatrix = new Matrix(); 
    // scaleMatrix.preScale(scaleX, scaleY); 
    scaleMatrix.setScale(scaleX, scaleY, middleX, middleY); 
    // scaleMatrix.postTranslate(-(bitmapWitdh)/2, -(bitmapHeight)/2); 
    // scaleMatrix.setTranslate(300, 300); 
    canvasImage.setMatrix(scaleMatrix); 
} 

這裏是什麼,我希望以後的高檔才達到(這是在屏幕上看到的用戶提供1280×720分辨率)Image Screen

釷是用戶得到的圖像分辨率(圖像分辨率2560x1920)Final image

+0

'然後從流中獲取可變位圖。對不起,你快點。從哪個流?從相機? – greenapps

+0

我沒有問題從相機獲取位圖。獲取它並將其設置爲畫布的位圖後開始。 是從onPictureTaken方法獲取數據的位圖。使用inMutable選項使其適合通過畫布繪製到它上面。但解決方案transfromation是我失敗的地方。 – Thracian

+0

我從位於相機的尺寸較大的位圖獲取位圖,而不是將文本繪製到其上的畫布。我高檔畫布匹配位圖尺寸,但文本和繪圖位置不是他們所支持的位置。 – Thracian

回答

0

對任何分辨率縮放畫布的解決方案只是使用Canvas.setScale()方法來縮放它。矩陣縮放應用於原點(默認爲屏幕左上角的(0,0)),它通過scaleX和scaleY因子將劃分尺寸轉換爲任何其他尺寸,這些尺寸可以通過將所需分辨率(所拍攝圖像的尺寸相機)的當前分辨率(設備分辨率)寬度和高度。

它基本上匹配捕獲的圖像和設備的屏幕尺寸。