相机预览无法启动,也不会显示任何错误

时间:2014-02-13 11:24:40

标签: android

我正在尝试开发相机活动,但是当我开始活动时,相机预览无法启动 它只显示按钮层,logcat不显示任何错误。

在带有android 4.1.1的10英寸平板电脑中,活动正常运行

在使用Android 4.0.4的海信4“中,活动不起作用(不启动相机预览)

有人可以帮助我吗?

这是活动中的代码

package com.asde.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Images;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ZoomControls;

public class CameraHandler extends Activity implements SurfaceHolder.Callback {

    private LayoutInflater myInflater = null;
    Camera myCamera;
    byte[] tempdata;
    boolean myPreviewRunning = false;
    private SurfaceHolder mySurfaceHolder;
    private SurfaceView mySurfaceView;
    ImageView takePicture;
    private int contador = 0;
    int currentZoomLevel = 0, maxZoomLevel = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_campreview);

        mySurfaceView = (SurfaceView) findViewById(R.id.surface);
        mySurfaceHolder = mySurfaceView.getHolder();
        mySurfaceHolder.addCallback(this);
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
            mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);



        myInflater = LayoutInflater.from(this);
        View overView = myInflater.inflate(R.layout.layer_camerahandler,null);
        this.addContentView(overView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));


        takePicture = (ImageView) findViewById(R.id.shutter);
        takePicture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myCamera.takePicture(myShutterCallback, myPictureCallback, myJpeg);
            }
        });

        ImageView patient = (ImageView) findViewById(R.id.patient);
        patient.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CameraHandler.this, WelcomeActivity.class);  
                startActivity(intent);
                finish();
            }
        });



    }

    //Evento post toma foto. (sonidos etc)
    ShutterCallback myShutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {
        }
    };

    PictureCallback myPictureCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera myCamera) {
            // TODO Auto-generated method stub
        }
    };

    PictureCallback myJpeg = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera myCamera) {
            // TODO Auto-generated method stub
            if(data != null){
                tempdata = data;
                done();
            }
        }
    };


    void done(){

        contador++;
        // Creo carpeta destino
        File folders = new File(Environment.getExternalStorageDirectory()
                + "/GASDE/Telemedicina/data/");
        folders.mkdirs();
        // Defino nombre archivo descarga
        File file;
        file = new File(Environment.getExternalStorageDirectory()
                + "/GASDE/Telemedicina/data/"+contador+".jpg");

        // Compruebo existencia archivo
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            file.delete();
            try {
                file.createNewFile();
            } catch (IOException e) {               
                e.printStackTrace();
            }
        }


        Bitmap bmp = BitmapFactory.decodeByteArray(tempdata, 0, tempdata.length);

        try {
               FileOutputStream out = new FileOutputStream(file);
               bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
               out.close();
               bmp.recycle();
               Toast.makeText(this, "creada", Toast.LENGTH_SHORT).show();
               myCamera.startPreview();
                System.out.println("2-" + myCamera.getParameters().getFocusMode());

        } catch (Exception e) {
               e.printStackTrace();
        }
    }

    //metodos de control del objeto camara

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
        try{
            if(myPreviewRunning){               
                myCamera.stopPreview();
                myPreviewRunning = false;
            }
            Camera.Parameters p = myCamera.getParameters();
            p.setPreviewSize(width,height);

            myCamera.setParameters(p);
            myCamera.setPreviewDisplay(holder);
            myCamera.startPreview();
            System.out.println("1-" + myCamera.getParameters().getFocusMode());
            myPreviewRunning = true;
        }catch(Exception e){}
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
            myCamera = Camera.open();

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            //myCamera.cancelAutoFocus();
            myCamera.stopPreview();
            myPreviewRunning = false;
            myCamera.release();
            myCamera = null;
        }

    }

这是Buttons的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation = "vertical"
     android:gravity = "right|center_vertical"
     android:layout_gravity = "right|center_vertical">

               <LinearLayout

          android:layout_width = "match_parent"
          android:layout_height = "wrap_content"
          android:orientation = "horizontal"
          android:layout_marginBottom="10dp"
          android:layout_marginLeft="10dp"
          android:layout_weight="1"
          android:gravity = "right|bottom">
          <ImageView
               android:id="@+id/zoomout"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:layout_marginRight="10dp"
               android:src="@drawable/zoomout">
          </ImageView>
           <ImageView
               android:id="@+id/zoomin"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:layout_marginRight="10dp"
               android:src="@drawable/zoomin">
          </ImageView>

     </LinearLayout>


     <LinearLayout

          android:layout_width = "match_parent"
          android:layout_height = "wrap_content"
          android:orientation = "horizontal"
          android:layout_weight="10"
          android:gravity = "right|center_vertical">

          <ImageView
               android:id="@+id/shutter"
               android:layout_width = "wrap_content"               
               android:layout_marginRight="8dp"
               android:layout_height = "wrap_content"
               android:src="@drawable/shutter">
          </ImageView>
     </LinearLayout>

          <LinearLayout

          android:layout_width = "match_parent"
          android:layout_height = "wrap_content"
          android:orientation = "horizontal"
          android:layout_marginBottom="10dp"
          android:layout_marginLeft="10dp"
          android:layout_weight="1"
          android:gravity = "left|bottom">
          <ImageView
               android:id="@+id/patient"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:src="@drawable/gallery">
          </ImageView>

     </LinearLayout>
</LinearLayout>

这是相机预览的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <SurfaceView
          android:id = "@+id/surface"
          android:layout_width = "fill_parent"
          android:layout_height = "fill_parent">
     </SurfaceView>
</LinearLayout>

0 个答案:

没有答案