我想绘制相机的预览图像,但由于某种原因它失败了。
我希望实时在表面颜色值上绘制一些线条。 代码:
///
// Update MVP matrix based on time
//
void Update ( ESContext *esContext, float deltaTime )
{
UserData *userData = (UserData*) esContext->userData;
ESMatrix perspective;
ESMatrix modelview;
float aspect;
// Compute a rotation angle based on time to rotate the cube
userData->angle += ( deltaTime * 40.0f );
if( userData->angle >= 360.0f )
userData->angle -= 360.0f;
// Compute the window aspect ratio
aspect = (GLfloat) esContext->width / (GLfloat) esContext->height;
// Generate a perspective matrix with a 60 degree FOV
esMatrixLoadIdentity( &perspective );
esPerspective( &perspective, 60.0f, aspect, 1.0f, 20.0f );
// Generate a model view matrix to rotate/translate the cube
esMatrixLoadIdentity( &modelview );
// Translate away from the viewer
esTranslate( &modelview, 0.0, 0.0, -2.0 );
// Rotate the cube
esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );
// Compute the final MVP by multiplying the
// modevleiw and perspective matrices together
esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );
}
抽奖后抛出错误:
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import java.io.IOException;
import java.util.List;
/**
* Created by Peter on 2015.12.03..
*/
public class PreviewBenji extends AppCompatActivity
{
Camera myCamera;
SurfaceView mySurfaceView;
SurfaceView mySurfaceView2;
SurfaceHolder mySurfaceHolder;
RelativeLayout baseLayout;
boolean isPreview;
int[] rgbints;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.previewbenji);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
isPreview = false;
mySurfaceView = (SurfaceView)findViewById(R.id.mypreview);
mySurfaceHolder = mySurfaceView.getHolder();
mySurfaceHolder.addCallback(mySurfaceCallback);
baseLayout = (RelativeLayout)findViewById(R.id.baseLayout);
//AugmentedView augmentedView = new AugmentedView(this);
//CameraPreview cameraPreview = new CameraPreview(this);
//baseLayout.addView(cameraPreview, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT));
/*
RelativeLayout baseLayout = (RelativeLayout)findViewById(R.id.baseLayout);
SurfaceView surfaceView = new MySurfaceView(this, null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
surfaceView.setLayoutParams(layoutParams);
baseLayout.addView(surfaceView);
*/
}
SurfaceHolder.Callback mySurfaceCallback = new SurfaceHolder.Callback()
{
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
/*
Camera.Parameters myParameters = myCamera.getParameters();
Camera.Size myBestSize = getBestPreviewSize(width, height, myParameters);
if(myBestSize != null)
{
myParameters.setPreviewSize(myBestSize.width, myBestSize.height);
myCamera.setParameters(myParameters);
myCamera.startPreview();
isPreview = true;
//Toast.makeText(getApplicationContext(), "Best Size:\n" + String.valueOf(myBestSize.width) + " : " + String.valueOf(myBestSize.height), Toast.LENGTH_LONG).show();
}
*/
}
/*
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
myCamera.setPreviewDisplay(mySurfaceHolder);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
public void surfaceCreated(SurfaceHolder holder)
{
mySurfaceView = (SurfaceView)findViewById(R.id.mypreview);
mySurfaceView2 = (SurfaceView)findViewById(R.id.mypreview2);
// camera setup
//myCamera = Camera.open();
//Camera.Parameters parameters = myCamera.getParameters();
//List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Parameters parameters = myCamera.getParameters();
final Camera.Size myBestSize = getBestPreviewSize(1920, 1080, parameters);
/*
for(int i=0; i<sizes.size(); i++)
{
Log.i("CS", i + " - width: " + sizes.get(i).width + " height: " + sizes.get(i).height + " size: " + (sizes.get(i).width * sizes.get(i).height));
}
*/
// change preview size
//final Camera.Size cs = sizes.get(8);
parameters.setPreviewSize(myBestSize.width, myBestSize.height);
// initialize image data array
rgbints = new int[myBestSize.width*myBestSize.height];
// make picture gray scale
parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
myCamera.setParameters(parameters);
// change display size
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) mySurfaceView.getLayoutParams();
params.height = (int) (mySurfaceView.getWidth()*myBestSize.height/myBestSize.width);
mySurfaceView.setLayoutParams(params);
//ViewGroup.LayoutParams overlayParams = (ViewGroup.LayoutParams) swOverlay.getLayoutParams();
//overlayParams.width = mySurfaceView.getWidth();
//overlayParams.height = mySurfaceView.getHeight();
//swOverlay.setLayoutParams(overlayParams);
try
{
myCamera.setPreviewDisplay(holder);
myCamera.setDisplayOrientation(0);
myCamera.startPreview();
}
catch (IOException e)
{
e.printStackTrace();
myCamera.stopPreview();
myCamera.release();
}
// callback every time a new frame is available
myCamera.setPreviewCallback(new Camera.PreviewCallback()
{
public void onPreviewFrame(byte[] data, Camera camera)
{
// create bitmap from camera preview
int pixel, pixVal, frameSize = myBestSize.width*myBestSize.height;
for(int i=0; i<frameSize; i++)
{
pixel = (0xff & ((int) data[i])) - 16;
if(pixel < 0x55)
{
pixVal = 0;
}
else
{
pixVal = 1;
}
rgbints[i] = pixVal;
}
int cp = rgbints[(int) (myBestSize.width*(0.5+(myBestSize.height/2)))];
//Log.i("CAMERA", "Center pixel RGB: "+cp);
Log.d("Center pixel: ", "" + cp);
// process preview image data
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
int start, finish, last;
start = finish = last = -1;
float x_ratio = mySurfaceView.getWidth()/myBestSize.width;
float y_ratio = mySurfaceView.getHeight()/myBestSize.height;
// display calculated path on overlay using canvas
Canvas overlayCanvas = mySurfaceHolder.lockCanvas();
overlayCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
// start by finding the tape from bottom of the screen
for(int y=myBestSize.height; y>0; y--)
{
for(int x=0; x<myBestSize.width; x++)
{
pixel = rgbints[y*myBestSize.height+x];
if(pixel == 1 && last == 0 && start == -1)
{
start = x;
}
else if(pixel == 0 && last == 1 && finish == -1)
{
finish = x;
break;
}
last = pixel;
}
overlayCanvas.drawLine(start * x_ratio, y * y_ratio, finish * x_ratio, y * y_ratio, paint);
start = finish = last = -1;
}
mySurfaceHolder.unlockCanvasAndPost(overlayCanvas);
}
});
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// TODO Auto-generated method stub
}
};
private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters)
{
Camera.Size bestSize = null;
List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();
bestSize = sizeList.get(0);
for(int i = 1; i < sizeList.size(); i++)
{
if((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height))
{
bestSize = sizeList.get(i);
}
}
return bestSize;
}
@Override
protected void onResume()
{
super.onResume();
myCamera = Camera.open();
}
@Override
protected void onPause()
{
if(isPreview)
{
myCamera.stopPreview();
}
myCamera.release();
myCamera = null;
isPreview = false;
super.onPause();
}
}
没有原生C可以吗? 我希望有人可以帮忙!