如何使突发模式可供摄像头可供、摄像头、模式

2023-09-12 22:03:50 作者:青春如此荒謬

我想建立一个突发模式相机小型照片应用程序。其主要思想是拍摄图像的每个0,3sec和存储图片在数组中,直到最后的拍摄。照片的数量,以应拍摄指定。

我只能拍一张照片每2秒。作为间隔最短。然后,当图像应写入存储,所述应用程序chrashes

是否有人知道如何实现这一点?这是我的code:

 进口java.io.ByteArrayInputStream中;
进口java.io.ByteArrayOutputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口java.io.ObjectInputStream中;
进口java.io.ObjectOutputStream中;
进口的java.util.ArrayList;
进口java.util.Iterator的;
进口的java.util.List;

进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.PixelFormat;
进口android.hardware.Camera;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口android.view.View;
进口android.view.Window;
进口android.view.WindowManager;
进口android.view.View.OnClickListener;

公共类CamaeaView扩展活动实现SurfaceHolder.Callback,OnClickListener {

    静态最终诠释FOTO_MODE = 1;
    私有静态最后字符串变量=测试;
    相机mCamera;
    布尔米previewRunning = FALSE;
    民营背景mContext =这一点;
    ArrayList的<对象>一个ListImage = NULL;
    公共静态的ArrayList<对象> List1的[];

    公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);

        Log.e(TAG的onCreate);

        @燮pressWarnings(未使用)
        捆绑额外= getIntent()getExtras()。

        。getWindow()和setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        的setContentView(R.layout.main);
        mSurfaceView =(SurfaceView)findViewById(R.id.surface_camera);
        mSurfaceView.setOnClickListener(本);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(本);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @覆盖
    保护无效onRestoreInstanceState(包savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
    }

    Camera.PictureCallback mPictureCallback =新Camera.PictureCallback(){
        公共无效onPictureTaken(byte []的为imageData,摄像机C){
            Log.d(开始,图像列表);

            意图mIntent =新意图();
            listImage.add(StoreByteImage(为imageData));
            SaveImage(ListImage对象);
            mCamera.start preVIEW();

            的setResult(FOTO_MODE,mIntent);
            完();

        }
    };

    保护无效onResume(){
        Log.d(TAG,onResume);
        super.onResume();
    }

    保护无效的onSaveInstanceState(包outState){
        super.onSaveInstanceState(outState);
    }

    保护无效的onStop(){
        Log.e(TAG的onStop);
        super.onStop();
    }

    公共无效surfaceCreated(SurfaceHolder持有者){
        Log.d(TAG,surfaceCreated);
        mCamera = Camera.open();

    }

    公共无效surfaceChanged(SurfaceHolder持有人,INT格式,INT W,INT高){
        Log.d(TAG,surfaceChanged);

        // XXX停止preVIEW()会崩溃,如果preVIEW没有运行
        如果(M previewRunning){
            mCamera.stop preVIEW();
        }

        Camera.Parameters p值= mCamera.getParameters();
        P.SET previewSize(W,H);
        p.setPictureSize(1024,768);
        p.getSupportedPictureFormats();
        p.setPictureFormat(PixelFormat.JPEG);
        mCamera.setParameters(对);
        尝试 {
            mCamera.set previewDisplay(保持器);
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        mCamera.start preVIEW();
        米previewRunning = TRUE;
    }

    公共无效surfaceDestroyed(SurfaceHolder持有者){
        Log.e(TAG,surfaceDestroyed);
        mCamera.stop preVIEW();
        米previewRunning = FALSE;
        mCamera.release();
    }

    私人SurfaceView mSurfaceView;
    私人SurfaceHolder mSurfaceHolder;

    公共无效的onClick(查看为arg0){
        INT I = 0;
        而(ⅰ4;){

            mCamera.takePicture(NULL,mPictureCallback,mPictureCallback);
            I = I + 1;
            尝试 {
                视频下载(3000);
            }赶上(InterruptedException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
        }
    }

    公共静态对象StoreByteImage(byte []的为imageData){

        obj对象= NULL;
        尝试 {
            ByteArrayInputStream的双=新ByteArrayInputStream的(为imageData);
            ObjectInputStream的objImageData =新的ObjectInputStream(之二);
            物镜= objImageData.readObject();

        }赶上(IOException异常前){
            // TODO:处理异常
        }赶上(ClassNotFoundException的前){
            // TODO:处理异常
        }
        返回OBJ;

    }

    公共静态对象createImagesArray(byte []的为imageData){

        obj对象= NULL;
        尝试 {
            ByteArrayInputStream的双=新ByteArrayInputStream的(为imageData);
            ObjectInputStream的objImageData =新的ObjectInputStream(之二);
            物镜= objImageData.readObject();

        }赶上(IOException异常前){
            // TODO:处理异常
        }赶上(ClassNotFoundException的前){
            // TODO:处理异常
        }
        返回OBJ;

    }

    公共静态布尔SaveImage(名单<对象>的ListImage){

        FileOutputStream中outStream = NULL;
        Log.d(LISTSIZE:,Integer.toString(listImage.size()));
        迭代器<对象> itList = listImage.iterator();
        byte []的imageBytes = NULL;
        而(itList.hasNext()){

            ByteArrayOutputStream BOS =新ByteArrayOutputStream();
            尝试 {
                ObjectOutputStream的OOS =新的ObjectOutputStream(BOS);
                oos.writeObject(ListImage对象);
                oos.flush();
                oos.close();
                bos.close();
                imageBytes = bos.toByteArray();
            }赶上(IOException异常前){
                // TODO:处理异常
            }

            尝试 {
                //写入SD卡
                outStream =新的FileOutputStream(的String.Format(
                        /sdcard/DCIM/%d.jpg,System.currentTimeMillis的())); //&其中9个;
                outStream.write(imageBytes);
                outStream.close();

            }赶上(FileNotFoundException异常E){//小于10>
                e.printStackTrace();
            }赶上(IOException异常E){
                e.printStackTrace();
            } 最后 {
            }
        }
        Log.d(TAG,onPictureTaken  -  JPEG);

        返回true;
    }

}
 

从注​​释错误:

  ERROR / MemoryHeapBase(1382):错误打开的/ dev / pmem_camera:没有这样的文件或目录
ERROR / QualcommCameraHardware(1382年):未能建立主堆PMEM池的/ dev / pmem_camera
ERROR / QualcommCameraHardware(1382):initRaw X发生故障,与pmem_camera,与pmem_adsp努力
 
利用AD7616灵活的可编程序列器和突发模式实现准同步采样

解决方案

AFAIK,你不能拍摄另一张照片,直到第一个完成。消除你的循环和视频下载()。您可以在接下来的画面 mPictureCallback

  

主要的想法是拍摄图像的每个0,3sec和存储图片在数组中,直到最后的拍摄。

在0,3sec的目标是速度比大多数设备可以处理图像,您可能没有为您的图像阵列足够的堆空间。我怀疑,你将不得不写每个图像到磁盘,因为它通过进来的的AsyncTask ,这样你就可以释放堆空间,同时也不会占用主应用程序主题。

I am trying to build a small photo app with a burst mode for camera. The main idea is to shoot a picture every 0,3sec and to store the pictures in an Array until the last picture is taken. The number of pictures to shoot should specified.

I can only shoot a picture every 2sec. as shortest interval. Then, when the pictures should be written to storage, the applications chrashes.

Does someone know how to implement this? Here is my code:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
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;

public class CamaeaView extends Activity implements SurfaceHolder.Callback, OnClickListener {

    static final int FOTO_MODE = 1;
    private static final String TAG = "Test";
    Camera mCamera;
    boolean mPreviewRunning = false;
    private Context mContext = this;
    ArrayList<Object> listImage = null;
    public static ArrayList<Object> List1[];

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Log.e(TAG, "onCreate");

        @SuppressWarnings("unused")
        Bundle extras = getIntent().getExtras();

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceView.setOnClickListener(this);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {
            Log.d("Start: ","Imagelist");

            Intent mIntent = new Intent();
            listImage.add(StoreByteImage(imageData));
            SaveImage(listImage);
            mCamera.startPreview();

            setResult(FOTO_MODE, mIntent);
            finish();

        }
    };

    protected void onResume() {
        Log.d(TAG, "onResume");
        super.onResume();
    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    protected void onStop() {
        Log.e(TAG, "onStop");
        super.onStop();
    }

    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated");
        mCamera = Camera.open();

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.d(TAG, "surfaceChanged");

        // XXX stopPreview() will crash if preview is not running
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        p.setPreviewSize(w, h);
        p.setPictureSize(1024, 768);
        p.getSupportedPictureFormats();
        p.setPictureFormat(PixelFormat.JPEG);
        mCamera.setParameters(p);
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e(TAG, "surfaceDestroyed");
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;

    public void onClick(View arg0) {
        int i = 0;
        while (i < 4) {

            mCamera.takePicture(null, mPictureCallback, mPictureCallback);
            i = i + 1;
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static Object StoreByteImage(byte[] imageData) {

        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(imageData);
            ObjectInputStream objImageData = new ObjectInputStream(bis);
            obj = objImageData.readObject();

        } catch (IOException ex) {
            // TODO: Handle the exception
        } catch (ClassNotFoundException ex) {
            // TODO: Handle the exception
        }
        return obj;

    }

    public static Object createImagesArray(byte[] imageData) {

        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(imageData);
            ObjectInputStream objImageData = new ObjectInputStream(bis);
            obj = objImageData.readObject();

        } catch (IOException ex) {
            // TODO: Handle the exception
        } catch (ClassNotFoundException ex) {
            // TODO: Handle the exception
        }
        return obj;

    }

    public static boolean SaveImage(List<Object> listImage) {

        FileOutputStream outStream = null;
        Log.d("ListSize: ", Integer.toString(listImage.size()));
        Iterator<Object> itList = listImage.iterator();
        byte[] imageBytes = null;
        while (itList.hasNext()) {

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                ObjectOutputStream oos = new ObjectOutputStream(bos);
                oos.writeObject(listImage);
                oos.flush();
                oos.close();
                bos.close();
                imageBytes = bos.toByteArray();
            } catch (IOException ex) {
                // TODO: Handle the exception
            }

            try {
                // Write to SD Card
                outStream = new FileOutputStream(String.format(
                        "/sdcard/DCIM/%d.jpg", System.currentTimeMillis())); // <9>
                outStream.write(imageBytes);
                outStream.close();

            } catch (FileNotFoundException e) { // <10>
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
        }
        Log.d(TAG, "onPictureTaken - jpeg");

        return true;
    }

}

Error from the comments:

ERROR/MemoryHeapBase(1382): error opening /dev/pmem_camera: No such file or directory 
ERROR/QualcommCameraHardware(1382): failed to construct master heap for pmem pool /dev/pmem_camera 
ERROR/QualcommCameraHardware(1382): initRaw X failed with pmem_camera, trying with pmem_adsp

解决方案

AFAIK, you cannot take another picture until the first one is complete. Eliminate your for loop and Thread.sleep(). Take the next picture in mPictureCallback.

The main idea is to shoot a picture every 0,3sec and to store the pictures in an Array until the last picture is taken.

The "0,3sec" objective is faster than most devices can process an image, and you may not have enough heap space for your array of images. I suspect that you will have to write each image out to disk as it comes in via an AsyncTask, so you can free up the heap space while also not tying up the main application thread.