在surfaceview中内置camera功能

第一次发技术博客,有些紧张,不知道从何说起,看官们就此看过,如有不妥,请大家多多指教。
这几天一直在看surfaceview的东西,surfaceview一般在播视频的时候会作为载体,现在当进入surfaceview时,将camera嵌入进去的操作。

少说废话,直接上代码吧。
package com.droidnova.android.tutorial.screenshot;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;

public class ScreenshotActivity extends Activity {
    private Panel mPanel;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mPanel = new Panel(this);
        setContentView(mPanel);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.screenshot:
                mPanel.saveScreenshot();
                break;
        }
        return true;
    }
}

主activity非常简单,重点是Panel这个类里面,用来嵌入camera功能。

package com.droidnova.android.tutorial.screenshot;

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

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.hardware.Camera;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

    public static float mWidth;
    public static float mHeight;

    private ViewThread mThread;
    private ArrayList<Element> mElements = new ArrayList<Element>();
    private int mElementNumber = 0;

    private Paint mPaint = new Paint();
    private String mScreenshotPath = Environment.getExternalStorageDirectory() + "/droidnova";
    private Camera mCamera;
    private ScreenshotActivity screenactivity;
    private SurfaceHolder mSurfaceHolder;
    private Context mcontext;


    public Panel(Context context) {

        super(context);
        mcontext = context;
        getHolder().addCallback(this);
//        mThread = new ViewThread(this);
        mPaint.setColor(Color.WHITE);
    }

    public void doDraw(long elapsed, Canvas canvas) {
//        canvas.drawColor(Color.BLACK);
//        synchronized (mElements) {
//            for (Element element : mElements) {
//                element.doDraw(canvas);
//            }
//        }
//        canvas.drawText("FPS: " + Math.round(1000f / elapsed) + " Elements: " + mElementNumber, 10,
//                10, mPaint);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//        mWidth = width;
//        mHeight = height;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPictureFormat(ImageFormat.JPEG);

        int pictureWidth = 800;
        int pictureHeight = 480;
        mSurfaceHolder = this.getHolder();
        parameters.setPictureSize(pictureWidth, pictureHeight);
        parameters.setPreviewSize(((Activity) mcontext).getWindowManager().getDefaultDisplay().getWidth(), ((Activity) mcontext).getWindowManager().getDefaultDisplay().getHeight());
        try {
            mCamera.setParameters(parameters);
            mCamera.setPreviewDisplay(mSurfaceHolder);
            mCamera.startPreview();
        } catch (RuntimeException o) {

        } catch (IOException i) {
            mCamera.release();
            mCamera = null;
        }

//        if (!mThread.isAlive()) {
//            mThread = new ViewThread(this);
//            mThread.setRunning(true);
//            mThread.start();
//        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
//        if (mThread.isAlive()) {
//            mThread.setRunning(false);
//        }
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        synchronized (mElements) {
//            mElements.add(new Element(getResources(), (int) event.getX(), (int) event.getY()));
//            mElementNumber = mElements.size();
//        }
        return super.onTouchEvent(event);
    }

    /**
     * If called, creates a screenshot and saves it as a JPG in the folder "droidnova" on the sdcard.
     */
    public void saveScreenshot() {
        if (ensureSDCardAccess()) {
            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            doDraw(1, canvas);
            File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg");
            Log.i("r123",  "123"+mScreenshotPath + "/" + System.currentTimeMillis());
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e("Panel", "FileNotFoundException", e);
            } catch (IOException e) {
                Log.e("Panel", "IOEception", e);
            }
        }
    }

    /**
     * Helper method to ensure that the given path exists.
     * TODO: check external storage state
     */
    private boolean ensureSDCardAccess() {
        File file = new File(mScreenshotPath);
        if (file.exists()) {
            return true;
        } else if (file.mkdirs()) {
            return true;
        }
        return false;
    }
}

ok,这样就可以实现简单的开机打开应用即可开启camera的功能了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值