【问题标题】:get JPG file from surfaceView android从surfaceView android获取JPG文件
【发布时间】:2013-07-19 15:31:44
【问题描述】:

我有几天试图在 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() 方法中。

希望有人可以帮助我解决任何问题。

非常感谢。

【问题讨论】:

    标签: android file bitmap camera surfaceview


    【解决方案1】:

    感谢您的帮助。

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

    现在,intent 中获取的图像已调整大小并加载到对象 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 是否会返回一些关于它的信息。

    我希望这可以帮助更多的人,我看到很多论坛都没有真正有效的答案。

    如果有任何反馈可以改进代码,我将不胜感激。

    再次感谢!

    【讨论】:

      【解决方案2】:

      我不知道您的位图是否有有效数据,但我可以告诉您执行您所要求的常见方法是 startActivityForResult 启动相机并取回图像:

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

      或者为相机预览附加一个回调,你可以在那里处理图像帧http://developer.android.com/reference/android/hardware/Camera.html#setPreviewCallback(android.hardware.Camera.PreviewCallback)

      如果您只想让用户拍照,那么我认为启动上述意图是您的解决方案。这是我编写的其中一个应用程序中的工作代码 sn-p:

          Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
      
          // we want the camera image to be put in a specific place
          File path;
          try { 
              path = new File(getCameraStoragePath());
          } catch (Exception e1) { 
              e1.printStackTrace();
              return;
          }                     
      
          SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
          mFilename = mPeer + "." + timeStampFormat.format(new java.util.Date()) + ".jpg";
      
          try
          { 
              mPath = path.getCanonicalPath();
          } catch (IOException e)
          { 
              e.printStackTrace();
          } 
      
          File out = new File(path, mFilename);
          android.net.Uri uri = android.net.Uri.fromFile(out);
      
          intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
          activity.startActivityForResult(intent, CAMERA);
      

      然后实现你自己的onActivityResult来处理用户拍摄的图片:

      protected void onActivityResult(int requestCode, int resultCode, Intent data);
      

      【讨论】:

        猜你喜欢
        • 2012-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多