Android的图片回拨数据为空 [英] Android picture call back data is null

查看:145
本文介绍了Android的图片回拨数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的相机code为Android拍照后的byte []为imageData参数为空,我不知道为什么。

 包com.pictures;

进口java.io.BufferedOutputStream;
进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;


进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.PixelFormat;
进口android.graphics.Bitmap.Com pressFormat;
进口android.hardware.Camera;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.KeyEvent;
进口android.view.MotionEvent;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口android.view.View;
进口android.view.Window;
进口android.view.WindowManager;
进口android.view.View.OnClickListener;
进口android.view.View.OnTouchListener;

公共类CamaraView扩展活动实现SurfaceHolder.Callback,
        OnClickListener {
    静态最终诠释FOTO_MODE = 0;
    私有静态最后字符串变量=CameraTest;
    相机mCamera;
    布尔米previewRunning = FALSE;
    民营背景mContext =这一点;

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

        Log.e(TAG的onCreate);

        捆绑额外= 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){

            如果(为imageData!= NULL){

                意图mIntent =新意图();

                StoreByteImage(mContext,为imageData,50,
                        ImageName);
                mCamera.start preVIEW();

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

            }
        }
    };

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

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

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

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

    }

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


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

        Camera.Parameters p值= mCamera.getParameters();
        P.SET previewSize(W,H);
        mCamera.setParameters(对);

        尝试 {
            mCamera.set previewDisplay(保持器);
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            的System.out.println(表面chagned捕获的异常);
            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){

        mCamera.takePicture(NULL,mPictureCallback,mPictureCallback);

    }

    公共静态布尔StoreByteImage(上下文mContext,byte []的为imageData,
            INT质量,串expName){

        文件sdImageMainDirectory =新的文件(/ SD卡);
        FileOutputStream中的FileOutputStream = NULL;
        字符串nameFile;
        尝试 {

            BitmapFactory.Options选项=新BitmapFactory.Options();
            options.inSampleSize = 5;

            位图MYIMAGE = BitmapFactory.de codeByteArray(为imageData,0,
                    imageData.length,期权);


            的FileOutputStream =新的FileOutputStream(
                    sdImageMainDirectory.toString()+/ image.jpg的);


            的BufferedOutputStream BOS =新的BufferedOutputStream(
                    FileOutputStream中);

            myImage.com preSS(比较pressFormat.JPEG,质量,BOS);

            bos.flush();
            bos.close();

        }赶上(FileNotFoundException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        返回true;
    }

}
 

解决方案

好吧,我看这个问题。要重复使用的图片回调RAW和JPEG回调。并从的javadoc,你可以阅读:

  

触发异步图像采集。将相机业务   启动一系列回调给应用程序,图像捕获   进展。拍摄图像后,会出现快门回调。   这可以被用来触发声音以让用户知道该图像   已被抓获。当原始图像数据是发生在原始回调   可用的(注:该数据将是无效的,如果没有原始图像   可用的回调缓冲器或原始图像回调缓冲器不   大到足以容纳原始图像)。发生在后览回调时,   缩放,完全处理后览图像可用(注意:不是所有的   硬件支持)。该JPEG回调发生在COM pressed   图像是可用的。如果该应用程序不需要特定   回调,空可以传递的,而不是一个回调方法。

所以,如果它被称为原始回调,数据可能为空。 如果你不喜欢的原始数据,只需要使用:

  mCamera.takePicture(NULL,NULL,mPictureCallback);
 

I am using camera code for Android after taking the picture the byte[] imageData parameter is null, I don't know why.

package com.pictures;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
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;
import android.view.View.OnTouchListener;

public class CamaraView extends Activity implements SurfaceHolder.Callback,
        OnClickListener {
    static final int FOTO_MODE = 0;
    private static final String TAG = "CameraTest";
    Camera mCamera;
    boolean mPreviewRunning = false;
    private Context mContext = this;

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

        Log.e(TAG, "onCreate");

        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) {

            if (imageData != null) {

                Intent mIntent = new Intent();

                StoreByteImage(mContext, imageData, 50,
                        "ImageName");
                mCamera.startPreview();

                setResult(FOTO_MODE, mIntent);
                finish();

            }
        }
    };

    protected void onResume() {
        Log.e(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.e(TAG, "surfaceCreated");
        mCamera = Camera.open();

    }

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


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

        Camera.Parameters p = mCamera.getParameters();
        p.setPreviewSize(w, h);
        mCamera.setParameters(p);

        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Caught exception in surface chagned");
            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) {

        mCamera.takePicture(null, mPictureCallback, mPictureCallback);

    }

    public static boolean StoreByteImage(Context mContext, byte[] imageData,
            int quality, String expName) {

        File sdImageMainDirectory = new File("/sdcard");
        FileOutputStream fileOutputStream = null;
        String nameFile;
        try {

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;

            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length,options);


            fileOutputStream = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/image.jpg");


            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return true;
    }

}

解决方案

Ok, I see the problem. You are reusing picture callback for both raw and jpeg callbacks. ANd from javadoc you can read:

Triggers an asynchronous image capture. The camera service will initiate a series of callbacks to the application as the image capture progresses. The shutter callback occurs after the image is captured. This can be used to trigger a sound to let the user know that image has been captured. The raw callback occurs when the raw image data is available (NOTE: the data will be null if there is no raw image callback buffer available or the raw image callback buffer is not large enough to hold the raw image). The postview callback occurs when a scaled, fully processed postview image is available (NOTE: not all hardware supports this). The jpeg callback occurs when the compressed image is available. If the application does not need a particular callback, a null can be passed instead of a callback method.

So if it was called as raw callback, data could be null. If you do not like raw data, just use:

 mCamera.takePicture(null, null, mPictureCallback);

这篇关于Android的图片回拨数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆