gpt4 book ai didi

Android缩小相机图片的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:37 24 4
gpt4 key购买 nike

更新可能会在新 Activity 中更改位图大小来解决问题

String myRef = this.getIntent().getStringExtra("filepath");
File imgFile = new File(myRef);

Log.e("BeatEmUp", myRef);
if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imagepunch);
myImage.setImageBitmap(myBitmap);

}

也许是这样的?

缩放的位图 = Bitmap.createScaledBitmap(bit, 200, 200, true);

或者下面是最好的方法

到目前为止,我的应用正在拍摄照片,然后使用 Intent 将照片传送过来并显示在新的 Activity 中。完成此操作后,我有一些代码可以使用 onClick 在顶部显示图像。问题是当拍摄图像时,我认为它是以最大尺寸拍摄的,所以我的应用程序正在强制关闭。

在我的 logcat 中,我得到 java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB) 我认为这意味着图像是太大了。

我已经通过在代码中放置一个较小的图像而不是拍摄的相机图像进行了测试,并且这种方法有效,这也让我回到了相机图片大小的问题。

所以我正在尝试实现 This From CommonsWare但到目前为止还没有运气。查看代码,我认为它会搜索最小的分辨率/尺寸,然后使用这些设置拍摄相机,我的想法是否正确?如果是这样,我该如何将其实现到我的代码中?

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

final int RESULT_SAVEIMAGE = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);



setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);

Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);



}});

}

ShutterCallback myShutterCallback = new ShutterCallback(){

public void onShutter() {
// TODO Auto-generated method stub

}};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub

}};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
int imageNum = 0;
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
imagesFolder.mkdirs(); // <----
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}

Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);


OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();

Toast.makeText(AndroidCamera.this,
"Image saved",
Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Intent intent = new Intent(getBaseContext(), Punch.class);
intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString());
//just using a request code of zero
int request=0;
startActivityForResult(intent,request);
}};

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}

if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
camera.release();
e.printStackTrace();
}
}
}





public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub

camera = Camera.open();
try {
Camera.Parameters parameters = camera.getParameters();

if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
// This is an undocumented although widely known feature
parameters.set("orientation", "portrait");
// For Android 2.2 and above
camera.setDisplayOrientation(90);
// Uncomment for Android 2.0 and above
parameters.setRotation(90);
} else {
// This is an undocumented although widely known feature
parameters.set("orientation", "landscape");
// For Android 2.2 and above
camera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}


camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();


}
camera.startPreview();

}


public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(previewing && camera != null) {
if(camera!=null) {
camera.stopPreview();
camera.release();
camera = null;
}
previewing = false;
}
}


}

最佳答案

So far I have my app which is taking a picture and then using an Intent to carry the picture over and display in a new Activity.

在大多数情况下,使用 Intent extras 在 Activity 之间传递数据是一个很好的解决方案。但是,大位图会因内存消耗而导致问题。在这种情况下,我会小心使用静态数据成员。

In my logcat I am getting java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB) which I think means that the image is too large.

这意味着无论您尝试做什么,您都没有足够的内存。

Looking through the code I think it searches for the smallest resouloution/size and then takes the camera using those settings am I right in thinking that

是的。

if so how can I implement that into my code?

复制和粘贴通常有效。 :-)

要告诉相机拍摄什么尺寸的照片,请在 Camera.Parameters 上使用 setPictureSize()。但是,您必须选择它支持的尺寸,并非所有设备都支持任意尺寸。调用 getSupportedPictureSizes() 会告诉您支持哪些尺寸。由您决定使用这些尺寸中的哪一个。在 the example I linked从你的另一个问题,我迭代这些尺寸并找到面积最小的一个。

话虽如此,如果图像大小的唯一问题是将其从一个 Activity 传递到另一个 Activity ,我会首先使用静态数据成员路由。当您不再需要该图像时,请务必null该静态数据成员。

Something like this maybe?

同样,这取决于您的目标是什么。当你只阐明了一个问题时,你似乎在四处寻找解决方案(而且,即使在那里,你也没有告诉我们哪里你遇到了内存不足的错误)。

如果您的目标是拍摄一张全分辨率照片,将其保存在一个文件中,然后在内存中保存一个缩略图,createScaledBitmap() 是一个很好的解决方案。

如果您的目标是首先拍摄缩略图图片(即,您不需要或不想要全分辨率图片),请使用 setPictureSize()

如果您的目标是始终使用全分辨率图片,请谨慎使用静态数据成员来消除任何不必要的 Bitmap 副本,看看这是否足够。它可能不是,这取决于您尝试对图像执行的操作。

关于Android缩小相机图片的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8757341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com