【问题标题】:Photo taken through camera intent is losing on quality通过相机意图拍摄的照片质量下降
【发布时间】:2016-03-19 11:07:21
【问题描述】:

类似的 SO 问题答案让我的应用程序崩溃。

我正在通过我的相机意图拍照并通过网络将其发送到我的 php 服务器,然后将其保存到目录中。它运行良好。但是正在保存的照片质量有问题(~20KB)。

我知道我的错误。 我阅读了 Android 文档,意识到我实际上是在发送照片缩略图而不是照片本身。这是我的代码

打开相机意图拍照。

addImage = (ImageButton) findViewById(R.id.imageButton);
addImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
}

接收图像。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
        photo = (Bitmap) data.getExtras().get("data");
        addImage.setImageDrawable(null);
        addImage.setBackgroundColor(Color.parseColor("#ffffff"));
        addImage.setImageBitmap(photo);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] photoByte = baos.toByteArray();
        encodedImage = Base64.encodeToString(photoByte,Base64.DEFAULT);

    }
}

我想要什么? 我只是想将我的原始图像(而不是缩略图)转换为 Base64 编码字符串(就像我对缩略图所做的那样)。

任何帮助将不胜感激。

[随意提出修改建议。 :)]

【问题讨论】:

    标签: android image android-intent android-camera photo


    【解决方案1】:

    我只是想转换我的原始图像(而不是缩略图)

    您没有“原始图像”,因为您没有在您的ACTION_IMAGE_CAPTURE Intent 中包含EXTRA_OUTPUT。添加它,然后你就有了全尺寸的图像(除了有问题的相机应用程序):

    /***
     Copyright (c) 2008-2016 CommonsWare, LLC
     Licensed under the Apache License, Version 2.0 (the "License"); you may not
     use this file except in compliance with the License. You may obtain a copy
     of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
     by applicable law or agreed to in writing, software distributed under the
     License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
     OF ANY KIND, either express or implied. See the License for the specific
     language governing permissions and limitations under the License.
    
     From _The Busy Coder's Guide to Android Development_
     https://commonsware.com/Android
     */
    
    package com.commonsware.android.camcon;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import java.io.File;
    
    public class CameraContentDemoActivity extends Activity {
      private static final String EXTRA_FILENAME=
        "com.commonsware.android.camcon.EXTRA_FILENAME";
      private static final String FILENAME="CameraContentDemo.jpeg";
      private static final int CONTENT_REQUEST=1337;
      private File output=null;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
        if (savedInstanceState==null) {
          File dir=
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    
          dir.mkdirs();
          output=new File(dir, FILENAME);
        }
        else {
          output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
        }
    
        if (output.exists()) {
          output.delete();
        }
    
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
    
        startActivityForResult(i, CONTENT_REQUEST);
      }
    
      @Override
      protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        outState.putSerializable(EXTRA_FILENAME, output);
      }
    
      @Override
      protected void onActivityResult(int requestCode, int resultCode,
                                      Intent data) {
        if (requestCode == CONTENT_REQUEST) {
          if (resultCode == RESULT_OK) {
            // spawn an IntentService to encode and upload your File
          }
        }
      }
    }
    

    【讨论】:

    • 几乎明白了。但是对这个错误有什么想法吗? java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {example.srujan.com.sendimagedemo/example.srujan.com.sendimagedemo.MainActivity}: java.lang.NullPointerException
    • @SrujanBarai:您的onActivityResult() 中某处存在错误,带有NullPointerException。堆栈跟踪应该向您显示您正在崩溃的代码行。
    • 我接受文件作为位图图像,我不应该接受。它在线photo = (Bitmap) data.getExtras().get("data"); 我知道问题所在。我不知道解决方案。
    • @SrujanBarai:删除该行,因为通常它不起作用,即使它起作用,它也会是缩略图。你知道图像应该在哪里。它位于您在EXTRA_OUTPUT 中指定的位置。上传该文件的内容。在IntentService 中执行此操作,因为这可能需要一些时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多