现在我正在开发相机应用程序。当我首先启动我的应用程序时它显示后置摄像头,当我点击该按钮时我会有一个按钮它将转向前置摄像头。那时我得到错误空指针异常。但是当我单独运行前置摄像头,完美显示前置摄像头。
This is my java code.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// getAspectRatio();
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
}});
front=(Button)findViewById(R.id.front);
front.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
{
Intent font_intent=new Intent(AndroidCamera.this,MainActivity.class);
startActivity(font_intent);
}
}
});
ShutterCallback myShutterCallback = new ShutterCallback(){
@Override
public void onShutter() {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1)
{
// TODO Auto-generated method stub
bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
Bitmap mBitmap2 = BitmapFactory.decodeResource(getResources(),currenteffect);
Bitmap newgerresized=getResizedBitmap(mBitmap2, bitmapPicture.getWidth(), bitmapPicture.getHeight());
Bitmap map = applyOverlayEffect(newgerresized, bitmapPicture);
String fieName = UUID.randomUUID().toString();
SaveImage(fieName, 100,map);
camera.startPreview();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
// TODO Auto-generated method stub
if(previewing)
{
camera.stopPreview();
previewing = false;
}
if (camera != null){
try
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try{
// TODO Auto-generated method stub
camera = Camera.open();
mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
}catch (Exception e) {
e.printStackTrace();
}finally
{
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
for front this is my java code
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion > android.os.Build.VERSION_CODES.FROYO){
id=findFrontFacingCamera();
Log.d("TestLedActivity","L'id trovato e': "+id);
camera = Camera.open(id+1);
} else{
Log.d("TestLedActivity","La versione e' froyo");
camera = Camera.open();
}
preview=(SurfaceView)findViewById(R.id.surfaceView1);
// TODO Auto-generated method stub
previewHolder=preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
i=(ImageView)findViewById(R.id.imageView1);
currenteffect=R.drawable.water;
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.water);
newgerresized=getResizedBitmap(mBitmap, width,height);
bi=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
paint = new Paint();
paint.setAlpha(100);
canvas=new Canvas(bi);
canvas.setBitmap(bi);
canvas.drawBitmap(newgerresized, 0, 0, paint);
i.setImageBitmap(bi);
}
public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
BitmapFactory.decodeStream(new ByteArrayInputStream(byteArray), null, options);
int srcWidth = bm.getWidth();
int srcHeight = bm.getHeight();
int desiredWidth = newWidth;
int desiredHeight = newHeight;
int inSampleSize = 1;
while(srcWidth / 2 > desiredWidth)
{
srcWidth /= 2;
srcHeight /= 2;
inSampleSize *= 2;
}
float desiredWidthScale = (float) desiredWidth / srcWidth;
float desiredHeightScale = (float) desiredHeight / srcHeight;
// Decode with inSampleSize
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = inSampleSize;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap sampledSrcBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(byteArray), null, options);
Matrix matrix = new Matrix();
matrix.postScale(desiredWidthScale, desiredHeightScale);
matrix.postRotate(Orientation);
Bitmap bmp = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);
return bmp;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
if (inPreview) {
camera.stopPreview();
}
camera.release();
camera=null;
inPreview=false;
super.onPause();
}
private int findFrontFacingCamera() {
int idCamera=0;
// Look for front-facing camera, using the Gingerbread API.
// Java reflection is used for backwards compatibility with pre-Gingerbread APIs.
try {
Class<?> cameraClass = Class.forName("android.hardware.Camera");
Object cameraInfo = null;
Field field = null;
int cameraCount = 0;
Method getNumberOfCamerasMethod = cameraClass.getMethod( "getNumberOfCameras" );
if ( getNumberOfCamerasMethod != null ) {
cameraCount = (Integer) getNumberOfCamerasMethod.invoke( null, (Object[]) null );
}
Class<?> cameraInfoClass = Class.forName("android.hardware.Camera$CameraInfo");
if ( cameraInfoClass != null ) {
cameraInfo = cameraInfoClass.newInstance();
}
if ( cameraInfo != null ) {
field = cameraInfo.getClass().getField( "facing" );
}
Method getCameraInfoMethod = cameraClass.getMethod( "getCameraInfo", Integer.TYPE, cameraInfoClass );
if ( getCameraInfoMethod != null && cameraInfoClass != null && field != null ) {
for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
getCameraInfoMethod.invoke( null, camIdx, cameraInfo );
int facing = field.getInt( cameraInfo );
if ( facing == 1 ) { // Camera.CameraInfo.CAMERA_FACING_FRONT
try {
Method cameraOpenMethod = cameraClass.getMethod( "open", Integer.TYPE );
if ( cameraOpenMethod != null ) {
Log.d("TestLedActivity","Id frontale trovato: "+camIdx);
//camera = (Camera) cameraOpenMethod.invoke( null, camIdx );
idCamera=camIdx;
}
} catch (RuntimeException e) {
Log.e("TestLedActivity", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
}
}
// Ignore the bevy of checked exceptions the Java Reflection API throws - if it fails, who cares.
catch ( ClassNotFoundException e ) {Log.e("TestLedActivity", "ClassNotFoundException" + e.getLocalizedMessage());}
catch ( NoSuchMethodException e ) {Log.e("TestLedActivity", "NoSuchMethodException" + e.getLocalizedMessage());}
catch ( NoSuchFieldException e ) {Log.e("TestLedActivity", "NoSuchFieldException" + e.getLocalizedMessage());}
catch ( IllegalAccessException e ) {Log.e("TestLedActivity", "IllegalAccessException" + e.getLocalizedMessage());}
catch ( InvocationTargetException e ) {Log.e("TestLedActivity", "InvocationTargetException" + e.getLocalizedMessage());}
catch ( InstantiationException e ) {Log.e("TestLedActivity", "InstantiationException" + e.getLocalizedMessage());}
catch ( SecurityException e ) {Log.e("TestLedActivity", "SecurityException" + e.getLocalizedMessage());}
if ( camera == null ) {
Log.d("TestLedActivity","Devo aprire la camera dietro");
// Try using the pre-Gingerbread APIs to open the camera.
idCamera=0;
}
return idCamera;
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result=null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
}
else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return(result);
}
SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(previewHolder);
}
catch (Throwable t) {
Log.e("PreviewDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast
.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width,
int height) {
Camera.Parameters parameters=camera.getParameters();
Camera.Size size=getBestPreviewSize(width, height,
parameters);
if (size!=null) {
//parameters.set("camera-id", 0);
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.startPreview();
inPreview=true;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};
}
please help me