gpt4 book ai didi

Android图片回调数据为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:44 24 4
gpt4 key购买 nike

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

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;
}

}

最佳答案

好的,我明白了。您正在为 raw 和 jpeg 回调重用图片回调。您可以从 javadoc 中阅读:

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.

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

 mCamera.takePicture(null, null, mPictureCallback);

关于Android图片回调数据为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7117906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com