从surfaceView android获取JPG文件
我有几天的时间尝试在安卓系统中拍摄一张照片,想法是在拍摄完照片后将文件上传到FTP。我有一个扩展SurfaceView的类,当它变得可见时,播放设备后摄像头的图像,在这里。点击相机的图像,我想保持JPEG格式的图像 我已经尝试了几十种解决方案,但是,我现在得到的是一个完全黑色的JPEG。我想我的方式是对的,但是缺少了一些东西 以下是我在自定义类的onClick事件中使用的代码:从surfaceView android获取JPG文件,android,file,bitmap,camera,surfaceview,Android,File,Bitmap,Camera,Surfaceview,我有几天的时间尝试在安卓系统中拍摄一张照片,想法是在拍摄完照片后将文件上传到FTP。我有一个扩展SurfaceView的类,当它变得可见时,播放设备后摄像头的图像,在这里。点击相机的图像,我想保持JPEG格式的图像 我已经尝试了几十种解决方案,但是,我现在得到的是一个完全黑色的JPEG。我想我的方式是对的,但是缺少了一些东西 以下是我在自定义类的onClick事件中使用的代码: try { //create bitmap screen capture Bi
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,选择了特定的大小,并将宽度和高度复制到这个。layoutParams要很好地适应,所有这些都在surfaceCreated()方法中
希望有人能帮助我任何迹象
非常感谢。我不知道您的位图是否包含有效数据,但我可以告诉您,执行您要求的操作的常用方法是启动TactivityResult或启动相机并获取图像:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
或者为相机预览附加回调,您可以在那里处理图像帧
如果您只想让用户拍照,那么我认为启动上述意图是您的解决方案。以下是我在编写的一个应用程序中使用的代码片段:
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);
我不知道您的位图是否有有效数据,但我可以告诉您,执行您要求的操作的常用方法是启动TactivityForresult启动相机并获取图像:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
或者为相机预览附加回调,您可以在那里处理图像帧
如果您只想让用户拍照,那么我认为启动上述意图是您的解决方案。以下是我在编写的一个应用程序中使用的代码片段:
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);
谢谢你的帮助 我终于接受了你的解决方案。我喜欢第一个选项,因为相机集成到应用程序中,更容易控制图像的旋转和预览的分辨率 现在,在意图中拍摄的图像已调整大小并加载到对象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。无论是横向还是纵向拍摄,我都会看看意图是否会返回一些相关信息
我希望这能帮助更多的人,我也看到过很多没有答案的论坛
如果您能提供任何反馈来完善代码,我将不胜感激
再次感谢 谢谢你的帮助 我终于接受了你的解决方案。我喜欢第一个选项,因为相机集成到应用程序中,更容易控制图像的旋转和预览的分辨率 现在,在意图中拍摄的图像已调整大小并加载到对象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。无论是横向还是纵向拍摄,我都会看看意图是否会返回一些相关信息
我希望这能帮助更多的人,我也看到过很多没有答案的论坛
如果您能提供任何反馈来完善代码,我将不胜感激
再次感谢