我有几天尝试在android中捕获照片,想法是一旦捕获到图像就将文件上传到FTP。我有一个扩展SurfaceView的类,当它变得可见时,可以播放设备后置摄像头的图像,在这里可以。单击相机的图像,我想将图像保留为JPEG格式。

我已经尝试了数十种解决方案,但是,我现在得到的是全黑的JPEG。我认为即时通讯方法正确,但是缺少某些内容。

这是我在自定义类的onClick事件中使用的代码:

    try {
        //create bitmap screen capture
        Bitmap bitmap;
        this.setDrawingCacheEnabled(true);
        //Freezes the image
        mCamera.stopPreview();
        bitmap = Bitmap.createBitmap(getDrawingCache(), 0, 0, this.getLayoutParams().width, this.getLayoutParams().height);
        this.setDrawingCacheEnabled(false);
        //Create temp file
        file = File.createTempFile("prueba", ".JPG", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
        //Copy bitmap content into file
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 60, fos);
        fos.flush();
        fos.close();
        //Free camera
        mCamera.release();
        mCamera = null;
    }catch(Exception e){
        e.printStackTrace();
    }


mCamera是android.hardware.Camera,选择了特定大小并将其宽度和高度复制到this.layoutParams以使其完全适合,所有这些都在surfaceCreated()方法中。

希望有人可以帮助我。

非常感谢你。

最佳答案

感谢您的帮助。

我终于接受了您的解决方案。我喜欢第一个选项,因为相机已集成到应用程序中,并且更容易控制图像的旋转和预览的分辨率。

至此,意图中拍摄的图像将调整大小并加载到对象ImagePreview(即ImageView)中。这是我的解决方案:

呼叫相机意图:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

if (getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size()>0) {
    pathImagen = android.net.Uri.fromFile(File.createTempFile("prueba"+System.currentTimeMillis(), ".JPG", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)));
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, pathImagen);
    startActivityForResult(intent, 94010);
}


获取图像文件并调整大小:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {

    case 94010:

        if (resultCode == Activity.RESULT_OK) {

            Bitmap bmp = null;

            try {

                //Obtenemos las dimensiones de la imagen (no se carga la imagen completa)
                ContentResolver cr = getContentResolver();
                cr.notifyChange(pathImagen, null);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Config.RGB_565;
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(pathImagen.getEncodedPath(), options);
                //bmp = android.provider.MediaStore.Images.Media.getBitmap(cr, pathImagen);
                System.out.println("Tamaño imagen: "+options.outWidth+"x"+options.outHeight);

                //Bitmap bmp = (Bitmap) data.getExtras().get("data");

                int anchoFinal = 480;
                int altoFinal  = (options.outHeight*480)/options.outWidth;

                options.inJustDecodeBounds = false;
                bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(pathImagen.getEncodedPath(), options), anchoFinal, altoFinal, false);
                System.out.println("Tamaño imagen despues escalar: "+bmp.getWidth()+"x"+bmp.getHeight());

                this.imagePreView.setVisibility(View.VISIBLE);
                this.imagePreView.getLayoutParams().width  = bmp.getWidth();
                this.imagePreView.getLayoutParams().height = bmp.getHeight();
                this.imagePreView.setImageBitmap(bmp);
                this.popupEditObject.setVisibility(View.VISIBLE);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


唯一的问题是,图像始终具有3264x1968的分辨率。无论是谁进行了水平或垂直o_O操作,我都会看到Intent是否返回有关此信息。

我希望这可以帮助更多的人,并且我已经看到许多论坛,但没有真正有效的答案。

我将不胜感激任何反馈以完善代码。

再次感谢!

07-28 02:20
查看更多