camera.startPreview()太慢了

时间:2014-06-01 19:18:45

标签: android performance camera

我想制作一个相机应用程序,在按下按钮时开始显示从相机到屏幕的图像,并在按钮释放时拍照。我做了一切,但相机启动速度太慢了。我希望它在我按下按钮时立即启动。我试图实例化我的相机,设置所有参数并将其流式传输为一个看不见的SurfaceView,但是当我按下按钮时,它似乎重新启动了相机。可能是什么问题?

这是我的代码:

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;    
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.view.View;
import android.graphics.AvoidXfermode.Mode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONException;
import org.json.JSONObject;

import com.shtern.selfy.MainActivity.JSONParse;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class CameraActivity extends Activity implements SurfaceHolder.Callback,
    View.OnTouchListener, Camera.PictureCallback, Camera.PreviewCallback {
private Camera camera;
private SurfaceHolder surfaceHolder;
private SurfaceView preview;
private Button shotBtn;
JSONObject json = new JSONObject();
JSONParse jsparse = new JSONParse();
String url = "http://myserverurl";
String userid;
String token;
String parentactivity;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    userid = getIntent().getStringExtra("userid").toString();
    token = getIntent().getStringExtra("token").toString();
    parentactivity = getIntent().getStringExtra("parent").toString();


    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);


    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.camera);


    preview = (SurfaceView) findViewById(R.id.SurfaceView01);

    surfaceHolder = preview.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


    shotBtn = (Button) findViewById(R.id.Button01);
    shotBtn.setOnTouchListener(this);
    camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
    // camera = Camera.open();
    Camera.Parameters params = camera.getParameters();
    params.setPictureSize(800, 480);
    params.setRotation(270);
    camera.setParameters(params);
    try {
        camera.setPreviewDisplay(surfaceHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        camera.setPreviewDisplay(surfaceHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    preview.setVisibility(View.VISIBLE);
    camera.startPreview();
    preview.setVisibility(View.VISIBLE);


}

@Override
protected void onResume() {
    super.onResume();

    preview.setVisibility(View.INVISIBLE);
}

@Override
protected void onPause() {
    super.onPause();

    if (camera != null) {
        camera.stopPreview();
        camera.setPreviewCallback(null);

        camera.release();
        camera = null;
    }
    preview.setVisibility(View.GONE);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.d("SURFACE", "CREATED");

    Size previewSize = camera.getParameters().getPreviewSize();
    float aspect = (float) previewSize.width / previewSize.height;

    int previewSurfaceWidth = preview.getWidth();
    int previewSurfaceHeight = preview.getHeight();

    LayoutParams lp = preview.getLayoutParams();

    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {


        camera.setDisplayOrientation(90);
        lp.height = previewSurfaceHeight;
        lp.width = (int) (previewSurfaceHeight / aspect);

    } else {


        camera.setDisplayOrientation(0);
        lp.width = previewSurfaceWidth;
        lp.height = (int) (previewSurfaceWidth / aspect);
    }

    preview.setLayoutParams(lp);

    try {
        camera.setPreviewDisplay(holder);
        camera.setPreviewCallback(this);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.setPreviewCallback(null);
    camera.release();
    camera = null;
    preview.setVisibility(View.GONE);
}

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        Log.d("TOUCH", "PUSHED");
        shotBtn.setVisibility(View.GONE);
        preview.setVisibility(View.VISIBLE);

        // start_camera();

    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        Log.d("TOUCH", "Release detected");

        camera.takePicture(null, null, null, this);
        //

    }
    return true;
}

@Override
public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera) {
    camera.stopPreview();
    camera.setPreviewCallback(null);
    preview.setVisibility(View.INVISIBLE);
    CameraActivity.this.finish();
    FrameLayout fl = (FrameLayout) findViewById(R.id.FrameLayout01);
    fl.removeAllViews();
    try {
        String path = CameraActivity.this.getFilesDir().getPath();
        Log.d("PATH", path);
        File saveDir = new File(path);

        if (!saveDir.exists()) {
            saveDir.mkdirs();
        }
        FileOutputStream os = new FileOutputStream(String.format(path
                + "/ava.jpg"));

        os.write(paramArrayOfByte);
        os.close();
    } catch (Exception e) {
    }


    jsparse.execute();

}

class JSONParse extends AsyncTask<Void, Void, JSONObject> {

    @Override
    protected JSONObject doInBackground(Void... params) {

        List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        String path = CameraActivity.this.getFilesDir().getPath();
        path = path + "/ava.jpg";
        Log.d("PATHINCAMERA", path);

        parameters.add(new BasicNameValuePair("user_id", userid));
        parameters.add(new BasicNameValuePair("token", token));
        parameters.add(new BasicNameValuePair("file", path));
        url = url + "/change_avatar_request/";

        postWithImage(url, parameters);
        if (parentactivity.equals("RegisterActivity")) {
            final Intent intent = new Intent(getApplicationContext(),
                    MenuActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("userid", String.valueOf(userid));
            intent.putExtra("token", String.valueOf(token));
            startActivity(intent);
        }
        // CameraActivity.this.finish();
        return null;
    }

    @Override
    protected void onPostExecute(JSONObject json) {


    }

}

public void postWithImage(String url, List<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        for (int index = 0; index < nameValuePairs.size(); index++) {
            if (nameValuePairs.get(index).getName()
                    .equalsIgnoreCase("file")) {
                // If the key equals to "image", we use FileBody to transfer
                // the data
                entity.addPart(nameValuePairs.get(index).getName(),
                        new FileBody(new File(nameValuePairs.get(index)
                                .getValue())));
            } else {
                // Normal string data
                entity.addPart(
                        nameValuePairs.get(index).getName(),
                        new StringBody(nameValuePairs.get(index).getValue()));
            }
        }

        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost, localContext);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onPreviewFrame(byte[] paramArrayOfByte, Camera paramCamera) {

}
}

2 个答案:

答案 0 :(得分:1)

要保持预览工作,请不要设置

preview.setVisibility(View.INVISIBLE)

相反,您可以暂时(直到按下按钮)覆盖具有非透明视图的预览,例如ImageView的。

答案 1 :(得分:0)

如果您想将图像设置为imageview,也许您可​​以使用此代码。您可以查看此project

PictureCallback jpegCallback = new PictureCallback() {
    @SuppressWarnings("deprecation")
    public void onPictureTaken(byte[] data, Camera camera) {

        Bitmap realImage;
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;
        options.inPurgeable=true;  
        options.inInputShareable=true;     
        realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);

        image.setImageBitmap(realImage);

    }