通过相机意图拍摄的照片质量下降
Photo taken through camera intent is losing on quality
类似的 SO 问答导致我的应用崩溃。
我正在通过我的相机意图拍摄一张照片,然后通过网络将其发送到我的 php 服务器,然后服务器将其保存到一个目录中。 工作正常。但是正在保存的照片质量下降 (~20KB)。
我知道我的错误。 我阅读了 Android 文档,意识到我实际上发送的是照片缩略图而不是照片本身。这是我的代码
打开 Camera Intent 拍照。
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 编码字符串(就像我对缩略图所做的那样)。
如有任何帮助,我们将不胜感激。
[随时提出修改建议。 :)]
I simply want to convert my original image (and not the thumbnail)
您没有 "original image",因为您没有在 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
}
}
}
}
类似的 SO 问答导致我的应用崩溃。
我正在通过我的相机意图拍摄一张照片,然后通过网络将其发送到我的 php 服务器,然后服务器将其保存到一个目录中。 工作正常。但是正在保存的照片质量下降 (~20KB)。
我知道我的错误。 我阅读了 Android 文档,意识到我实际上发送的是照片缩略图而不是照片本身。这是我的代码
打开 Camera Intent 拍照。
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 编码字符串(就像我对缩略图所做的那样)。
如有任何帮助,我们将不胜感激。
[随时提出修改建议。 :)]
I simply want to convert my original image (and not the thumbnail)
您没有 "original image",因为您没有在 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
}
}
}
}