如何在Android中拥有像摄像头集成的whatsapp

时间:2014-10-14 05:01:49

标签: java android

我正在开展一个项目,我需要从应用程序中获取图像,然后将其上传到服务器。我希望用户能够确认图像是否被接受,如whatsapp。我有什么到目前为止尝试使用的是surfaceview。代码如下所示:

package com.example.surfaceviewcamera;

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

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    SurfaceView mSurfaceView;
    SurfaceHolder mSurfaceHolder;
    Camera mCamera;
    boolean mPreviewRunning;
    Button btncapture;
    String fullPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btncapture=(Button) findViewById(R.id.btncapture);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        btncapture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                mCamera.takePicture(null, null, mPictureCallback);

            }
        });

    }
     Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
            public void onPictureTaken(byte[] imageData, Camera c) {

                Bitmap bitmap = BitmapFactory.decodeByteArray(imageData , 0, imageData .length);
                       saveImageToExternalStorage(bitmap);
                Toast.makeText(getApplicationContext(),"Image stored succesfully at "+fullPath,Toast.LENGTH_LONG).show();
                mCamera.startPreview();
            }
        };
        public void saveImageToExternalStorage(Bitmap image) {
            //image=scaleCenterCrop(image,200,200);
             fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
            try
            {
            File dir = new File(fullPath);
            if (!dir.exists()) {
            dir.mkdirs();
            }
            OutputStream fOut = null;
            File file = new File(fullPath, "photo_marina200000.jpg");

            if(file.exists())
            file.delete();

            file.createNewFile();
            fOut = new FileOutputStream(file);
            // 100 means no compression, the lower you go, the stronger the compression
            image.compress(Bitmap.CompressFormat.JPEG, 50, fOut);

            fOut.flush();
            fOut.close();
            }
            catch (Exception e)
            {
            Log.e("saveToExternalStorage()", e.getMessage());
            }
            }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera=Camera.open();
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        //mCamera.setDisplayOrientation(180);
    }  if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
        mCamera.setDisplayOrientation(90);
    }


    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w,
            int h) {
        if (mPreviewRunning) {
            mCamera.stopPreview();
            }
            Camera.Parameters p = mCamera.getParameters();
            p.setPreviewSize(w, h);
            mCamera.setParameters(p);
            try {
            mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
            e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;

    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();

    }

}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
 android:layout_height="fill_parent"
android:orientation="vertical">

<SurfaceView
 android:id="@+id/surface_camera"
android:layout_width="fill_parent"
 android:layout_height="10dip"
android:layout_weight="1">
</SurfaceView>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CAPTURE IMAGE"
    android:id="@+id/btncapture"
    android:layout_gravity="center"
    />
</LinearLayout>

要求是用户不应该使用手机的默认相机应用程序。这就是为什么我使用了surfaceview。我的问题是如何有一个确认和拒绝按钮,如whatsapp.Can任何人建议比我上面做了什么?

0 个答案:

没有答案