1

我正在尝试在表面视图上捕获相机的预览,以将其保存为内部存储器中的 JPEG。我在这个网站上找到了一些代码,这主要是我想要的,但将图像保存到 SD 卡。我改变了它,并提出了以下代码。

Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback()
{
    @Override
    public void onPreviewFrame( byte[] data, Camera Cam ) {
        //Log.d(TAG, "FRAME");
        Camera.Parameters parameters = Cam.getParameters();
        int format = parameters.getPreviewFormat();
        //Log.d(TAG, "FORMAT:" + format);
        //YUV formats require more conversion
        if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {
            int w = parameters.getPreviewSize().width;
            int h = parameters.getPreviewSize().height;
            // Get the YuV image
            YuvImage yuv_image = new YuvImage(data, format, w, h, null);
            // Convert YuV to Jpeg
            Rect rect = new Rect(0, 0, w, h);
            ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
            yuv_image.compressToJpeg(rect, 100, output_stream);
            byte[] byt = output_stream.toByteArray();
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream("/data/data/com.example.max.camtest/files/test"+System.currentTimeMillis()+".jpg");
                outStream.write(byt);
                outStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }


        }
    }
};

在surfaceview上显示预览并触发mPrevCallback。它成功保存了不同大小(250~500Kb)但都是黑色的图片。当我尝试使用 camera.takePicture 功能拍摄照片时,它也是黑色的。

我究竟做错了什么?我该如何调试呢?谢谢!

4

1 回答 1

0

使用此意图拍照

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = new File(android.os.Environment.getExternalStorageDirectory(), AppInfo.getInstance().getCurrentLoginUserInfo().getId()+".jpg");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 1);

并在您的活动结果中...... Note Bitmap bitmap = getScaledBitmap(uri.getPath(), 200, true);200 是您的最大图像尺寸。

if(requestCode == 1)
        {
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
            final String imgPath = base + "/" +AppInfo.getInstance().getCurrentLoginUserInfo().getId()+".jpg";
            File file = new File(imgPath);
            if (file.exists()) 
            {
                Uri uri = Uri.fromFile(file);

            Log.d(TAG, "Image Uri path: " + uri.getPath());

            Bitmap bitmap = getScaledBitmap(uri.getPath(), 200, true);

        }}

此方法将在调整大小后返回图像位图-

private Bitmap getScaledBitmap(String imagePath, float maxImageSize, boolean filter) {

FileInputStream in;
BufferedInputStream buf;

try {
    in = new FileInputStream(imagePath);

    buf = new BufferedInputStream(in);
    Bitmap realImage = BitmapFactory.decodeStream(buf);


    float ratio = Math.min(
            (float) maxImageSize / realImage.getWidth(),
            (float) maxImageSize / realImage.getHeight());

    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, height, filter);
    return newBitmap;

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

return null;
}

现在您已经缩放了位图图像。

希望这会帮助你。

于 2015-05-14T17:02:10.447 回答