操作相机的预览位图

时间:2012-11-22 04:33:48

标签: android graphics camera

我正在尝试创建一个类似于此处的实时Kaleidoscope应用:http://www.windowsphone.com/en-gb/store/app/live-kaleidoscope/32e41ccf-5a25-42ea-84ff-07688f3e6aa5

为此,我已经能够获得与相机预览相对应的位图。我试图从三角形路径创建一个多边形,并在使用矩阵变换操作位图后用位图填充路径。我还没有研究那些转换,但是现在我甚至没有看到多边形和它们内部的图像。我的代码中没有任何错误。当我运行应用程序时,我只看到相机正在运行。我卡住了!请帮忙!这是我的代码:

公共类Kaleidoscope扩展了View

{

Bitmap imageData;
Canvas canvas1;
int picWidth, picHeight, ang, displayWidth, displayHeight, numOfsides;
float triangleSide, triangleHeight;

public Kaleidoscope(Context context) {
    super(context);
}

public Kaleidoscope(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void init(Bitmap image, int sides, int w, int h) {

    numOfsides = sides;
    triangleSide = (w - 100) / 2;
    // triangleHeight=(float)Math.tan(Math.PI/3)*triangleSide/2;
    displayWidth = w;
    displayHeight = h;
    imageData = image;
    draw();
}

public void onDraw(Canvas canvas) {
    canvas1 = canvas;
}

public void draw() {

    float angle = 360 / numOfsides;
    float totalAngle = 0;
    float centerX = displayWidth / 2;
    float centerY = displayHeight / 2;
    float cornerX = centerX + triangleSide;
    float cornerY = centerY / 2;
    Matrix mat = new Matrix();

    mat.postTranslate(picWidth / 2, 0);

    // Matrix mat2=mat;
    //
    // float concatArray[]={-1,0,0,1,0,0};
    // Matrix concatMat=new Matrix();
    // concatMat.setValues(concatArray);
    // mat2.postConcat(concatMat);

    for (int i = 0; i < numOfsides; i++) {

        float verticalDistance = (triangleSide * (float) (Math.sin(Math
                .toRadians(totalAngle))));
        float horizontalDistance = (triangleSide * (float) (Math.cos(Math
                .toRadians(totalAngle))));

        float x = (displayWidth / 2) + horizontalDistance - (picWidth / 2);
        float y = (displayHeight / 2) + verticalDistance - (picHeight / 2);

        Path triangle = new Path();
        triangle.lineTo(cornerX, cornerY);
        triangle.lineTo(x, y);
        triangle.lineTo(centerX, centerY);
        cornerX = x;
        cornerY = y;
        totalAngle = totalAngle + angle;
        Bitmap matBitmap;

        if (i % 2 == 0) {
            matBitmap = Bitmap.createBitmap(imageData, 0, 0,
                    imageData.getWidth(), imageData.getHeight(), mat, true);
        } else {
            matBitmap = Bitmap.createBitmap(imageData, 0, 0,
                    imageData.getWidth(), imageData.getHeight(), mat, true);
        }

        BitmapShader fillBMPshader = new BitmapShader(matBitmap,
                Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
        Paint fill = new Paint();
        fill.setColor(0xFFFFFFFF);
        fill.setStyle(Paint.Style.FILL);
        fill.setShader(fillBMPshader);

        canvas1.drawPath(triangle, fill);

    }
}

}

1 个答案:

答案 0 :(得分:0)

首先,抱歉我的英文写作。 (因为我是韩国人)

如果您在Android中使用相机,名为preview的类将扩展SurfaceView并实现SurfaceHolder.callback

然后mainActivity调用此类。正确?

mainActivity调用preview类后,您必须创建另一个扩展View类的类。

使用名为onDraw的方法覆盖:onDraw(Canvas canvas)并在此方法中创建源代码,如下所示:

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setTextSize(40);
canvas.drawText("X", canvas.getWidth() / 2, canvas.getHeight() / 2, mPaint);

然后返回mainActivity并执行addView

此来源中“视图类”的名称为“test”。

final FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
if (camview == null) {
    camview = new Preview(this);
    frame.addView(camview);
    test = new InvalidateTest(context);
    frame.addView(test, new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

制作一个单词,只需创建一个新类并在(框架)布局中添加此视图。