首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我搞不懂为什么这段代码不能在Android中显示图片

我搞不懂为什么这段代码不能在Android中显示图片
EN

Stack Overflow用户
提问于 2010-07-17 03:49:59
回答 1查看 730关注 0票数 0

我非常关注TicTacToeLib参考资料中的示例代码,以及一本名为“Android应用程序开发”的书中有关如何显示位图的示例。我正在做他们正在做的所有相同的事情,唯一的区别是我使用了SurfaceView而不是视图。有人能告诉我我哪里做错了吗?最终目标是手动显示相机的预览图像,并在其上方显示图像。

对不起,我忘了包括问题是什么。onDraw方法永远不会被调用。在我的印象中,它应该由无效方法调用。其次,lockCanvas()方法返回一个空画布。即使我修复了代码,使其不使用SurfaceHolder中的PUSH_BUFFER常量(我已经为此更正编辑了代码)。

代码语言:javascript
复制
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();
}


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-21 02:15:20

显然我找到了一个解决方案。尽管SurfaceView继承自View,但它并不像View那样调用onDraw。至于为什么lockCanvas不工作,我仍然不知道。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3268363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档