我想制作一个相机应用程序,该应用程序在按下按钮时开始在相机到屏幕之间显示图像,并在释放按钮时拍照。我已经完成了所有操作,但是相机的启动速度太慢。我希望它在我按下按钮时立即启动。我尝试实例化我的相机,设置所有参数,并将其流式传输为不可见的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) {

}
}

最佳答案

要保持预览正常工作,您不应该设置

preview.setVisibility(View.INVISIBLE)


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

关于android - camera.startPreview()太慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23983527/

10-10 08:24