2010-07-16 5 views
0

TicTacToeLib 용 리소스의 샘플 코드와 Android 애플리케이션이라는 제목의 비트 맵을 표시하는 방법에 대한 예제를 매우 자세히 검토하고 있습니다. 개발. 나는 그들이하는 일과 똑같은 일을하고 있습니다. 유일한 차이점은 뷰 대신에 SurfaceView를 사용하고 있다는 것입니다. 아무도 내가 뭘 잘못하고 있다고 말할 수 있습니까? 최종 목표는 수동으로 카메라의 미리보기 이미지를 표시하고 이미지를 위에 표시하는 것입니다.이 코드가 Android에서 이미지를 표시하지 않는 이유를 알아낼 수 없습니다.

죄송합니다. 문제가 무엇인지 포함하는 것을 잊었습니다. onDraw 메서드는 지금까지 호출되지 않습니다. 무효화 메소드가 호출해야한다는 인상 아래에있었습니다. 둘째로 lockCanvas() 메서드가 NULL 캔버스를 반환합니다. SurfaceHolder에서 PUSH_BUFFER 상수를 사용하지 않도록 코드를 수정 한 후에도 (이 수정 코드를 편집했습니다).

package com.test.combine; 

import android.content.res.XmlResourceParser; 
import android.app.Activity; 
import android.content.Context; 
import android.hardware.Camera; 
import android.hardware.Camera.Size; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.Window; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import java.io.IOException; 
import android.util.Log; 
import java.util.List; 

public class CombineTestActivity extends Activity 
{ 
private Preview mPreview; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Hide the window title. 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    // Create our Preview view and set it as the content of our activity. 
    mPreview = new Preview(this); 
    setContentView(mPreview); 
} 

} 

// ---------------------------------------------------------------------- 

class Preview extends SurfaceView implements SurfaceHolder.Callback { 
private SurfaceHolder mHolder; 
private Camera mCamera; 
private Paint mPaint; 
private Canvas canvas; 
private Bitmap currentprev; 
private Picture mPicture; 

private Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback() 
{ 
     public void onPreviewFrame(byte[] data, Camera Cam) { 
       Log.d("CombineTestActivity", "Preview registered"); 
       Log.d("CombineTestActivity", "Data length = " 
         + data.length); 
       // currentprev = BitmapFactory.decodeByteArray(data, 0, 
       //  data.length); 

       currentprev = BitmapFactory.decodeResource(getResources(), 
        R.drawable.creature00); 
       if(currentprev == null) 
        Log.d("CombineTestActivity", "currentprev is null"); 

       Log.d("CombineTestActivity", "Preview Finished"); 
       invalidate(); 
     } 
}; 

private OnTouchListener mCorkyListener = new OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent me) { 
       Log.d("CombineTestActivity", "touch registered"); 
       mCamera.takePicture(null, null, mPicCallback); 
       return false; 
     } 
}; 

private Camera.PictureCallback mPicCallback = new Camera.PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera mCamera) { 

       Log.d("CombineTestActivity", "picture method run"); 
     } 
}; 

Preview(Context context) { 
    super(context); 

    // Install a SurfaceHolder.Callback so we get notified when the 
    // underlying surface is created and destroyed. 
mPicture = new Picture(); 
    mHolder = getHolder(); 
    mHolder.addCallback(this); 
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL); 
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    canvas = mHolder.lockCanvas(); 
    if(canvas==null) 
     Log.d("CombineTestActivity", "canvas is null in constructor"); 

this.setOnTouchListener(mCorkyListener); 

invalidate(); 

} 

@Override 
protected void onDraw(Canvas mCanvas) { 

     if(mCanvas == null) 
       Log.e("CombineTestActivity", "canvas is null"); 
     if(currentprev == null) { 
       Log.e("CombineTestActivity", "currentprev is null"); 
       return; 
     } 

     mCanvas.drawBitmap(currentprev, 0.0f, 0.0f, mPaint); 

     Log.d("CombineTestActivity", "exiting onDraw"); 

} 


public void surfaceCreated(SurfaceHolder holder) { 
    // The Surface has been created, acquire the camera and tell it where 
    // to draw. 

    mCamera = Camera.open(); 
    mCamera.setPreviewCallback(mPrevCallback); 

    /*   
    try { 
     mCamera.setPreviewDisplay(holder); 
    } catch (IOException exception) { 
     mCamera.release(); 
     mCamera = null; 
     // TODO: add more exception handling logic here 
    } 
    */  

} 

public void surfaceDestroyed(SurfaceHolder holder) { 
    // Surface will be destroyed when we return, so stop the preview. 
    // Because the CameraDevice object is not a shared resource, it's very 
    // important to release it when the activity is paused. 
    mCamera.stopPreview(); 
    mCamera.release(); 
    mCamera = null; 
} 

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { 
    final double ASPECT_TOLERANCE = 0.05; 
    double targetRatio = (double) w/h; 
    if (sizes == null) return null; 

    Size optimalSize = null; 
    double minDiff = Double.MAX_VALUE; 

    int targetHeight = h; 

    // Try to find an size match aspect ratio and size 
    for (Size size : sizes) { 
     double ratio = (double) size.width/size.height; 
     if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; 
     if (Math.abs(size.height - targetHeight) < minDiff) { 
      optimalSize = size; 
      minDiff = Math.abs(size.height - targetHeight); 
     } 
    } 

    // Cannot find the one match the aspect ratio, ignore the requirement 
    if (optimalSize == null) { 
     minDiff = Double.MAX_VALUE; 
     for (Size size : sizes) { 
      if (Math.abs(size.height - targetHeight) < minDiff) { 
       optimalSize = size; 
       minDiff = Math.abs(size.height - targetHeight); 
      } 
     } 
    } 
    return optimalSize; 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    // Now that the size is known, set up the camera parameters and begin 
    // the preview. 
    Camera.Parameters parameters = mCamera.getParameters(); 

    List<Size> sizes = parameters.getSupportedPreviewSizes(); 
    if(sizes.isEmpty()) 
     Log.d("CameraTestActivity", "sizes null"); 
    else 
     Log.d("CameraTestActivity", "sizes not null"); 

    Size optimalSize = getOptimalPreviewSize(sizes, w, h); 
    if(optimalSize == null) 
     Log.d("CameraTestActivity", "optimalSize null"); 
    else 
     Log.d("CameraTestActivity", "optimalSize not null"); 


    parameters.setPreviewSize(optimalSize.width, optimalSize.height); 

if(parameters == null) 
    Log.d("CameraTestActivity", "parameters null"); 
else 
    Log.d("CameraTestActivity", "parameters not null"); 

    mCamera.setParameters(parameters); 
    mCamera.startPreview(); 
} 


} 
+0

이 코드를 실행하면 정확히 무엇이 발생합니까? 임의의 추측으로, SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS를 사용하면 안됩니다. –

+0

실제로 몇 가지 일이 벌어지고 있습니다. (미안합니다.) 하나의 onDraw는 결코 호출되지 않습니다. 두 번째 surfaceholder.lockCanvas()는 null을 반환합니다. 나는 당신이 추천 한 것을 들여다 보았고 그 라인을 아무것도 바꾸지 않았고 캔버스가 여전히 null로 나왔다. 다음 SURFACE_TYPE_NORMAL 사용하여 시도하고 여전히 null 나온. – RyoxSinfar

답변

0

분명히 해결책을 찾았습니다. SurfaceView는 View에서 상속 받지만 View처럼 onDraw를 호출하지 않습니다. lockCanvas가 작동하지 않는 이유는 무엇입니까?

관련 문제