标签: android-bitmap

Bitmap.compress 花费太多时间(约 2 秒)

位图压缩过程需要太多时间。我该如何解决这个问题?

在活动中:

icon= BitmapFactory.decodeResource(getResources(),R.mipmap.image);
Run Code Online (Sandbox Code Playgroud)

在回调类中:

        synchronized (holder) {
            stream = new ByteArrayOutputStream();
            Log.d("LIFE_CYCLE", "settingImage 1=" + System.currentTimeMillis());
            icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
            Log.d("LIFE_CYCLE", "settingImage 2=" + System.currentTimeMillis());
            byteArray = stream.toByteArray();
            b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            if(mWidth<mHeight){
                icon= Bitmap.createScaledBitmap(b, (int)(mWidth*0.75), (int)(mWidth*0.75), false);
            }
            else{
                icon= Bitmap.createScaledBitmap(b, (int)(mHeight*0.75), (int)(mHeight*0.75), false);
            }

            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            canvas.drawBitmap(icon, ((mWidth)-icon.getWidth())/2, (mHeight-icon.getHeight())/2, new Paint());
            draw_target(canvas);
        }
Run Code Online (Sandbox Code Playgroud)

此行大约需要 2 秒:

icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
Run Code Online (Sandbox Code Playgroud)

PS我的图像是部分透明的,所以我需要使用.PNG而不是.JPG

android bitmap bitmapimage bitmapfactory android-bitmap

1
推荐指数
1
解决办法
1497
查看次数

调整位图大小保持纵横比且不变形和裁剪

有没有办法在不变形的情况下调整位图大小,使其不超过 720*1280 ?较小的高度和宽度完全没问题(在较小宽度或高度的情况下空白画布很好)我尝试了这个/sf/answers/1080891801/,但它给出了扭曲的图像。有人能提出更好的解决方案吗?

android bitmap android-imageview android-bitmap

1
推荐指数
1
解决办法
4076
查看次数

将图像传递给另一个活动时,图像会丢失原始结果

我正在申请我的相机功能.我正在捕捉图像并将其传递给另一个活动.我现在面临的问题是,当我在另一个活动展示形象就失去原来的结果(获得异样)出于某种原因.这就是我这样做的方式:

private void takePhotoFromCamera() {
        if(ActivityCompat.checkSelfPermission(EnterDataView.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }else {
            String[] permissionRequest = {Manifest.permission.CAMERA};
            requestPermissions(permissionRequest, 8675309);
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
            if(requestCode == CAMERA_REQUEST){
                Bitmap mphoto = (Bitmap) data.getExtras().get("data");

                Intent passPhoto = new Intent(this, Photo.class);
                passPhoto.putExtra("byteArray",mphoto);

                passPhoto.putExtra("Caller", getIntent().getComponent().getClassName());
                startActivity(passPhoto);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在其他活动中获取图像:

if(getIntent().hasExtra("byteArray")) {
            //ImageView _imv= new ImageView(this);
            /*Bitmap _bitmap = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);*/
            Intent intent_camera = getIntent();
            Bitmap …
Run Code Online (Sandbox Code Playgroud)

android android-camera android-camera-intent android-bitmap

1
推荐指数
1
解决办法
221
查看次数

从文件路径获取位图

所以问题是我想从绝对路径获取位图,所以我将这些路径传递ArrayList<Strings>给我的演示者,在那里我有下一段代码:

private void decodeImageUri(final ArrayList<String> imageUris) {

    while(imageCounter < imageUris.size()) {
        DecodeBitmapsThreadPool.post(new Runnable() {
            @Override
            public void run() {

                Bitmap bitmap = BitmapFactory.decodeFile(imageUris.get(imageCounter));

                mImagesBase64Array.add(bitmapToBase64(bitmap));
            }
        });
    }
    DecodeBitmapsThreadPool.finish();
    Log.d("SIZE OF BASE64", " ---------- " + mImagesBase64Array.size());

}
Run Code Online (Sandbox Code Playgroud)

这是我的 ThreadPool 类:

public class DecodeBitmapsThreadPool {

private static DecodeBitmapsThreadPool mInstance;
private ThreadPoolExecutor mThreadPoolExec;
private static int MAX_POOL_SIZE;
private static final int KEEP_ALIVE = 10;
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();

public static synchronized void post(Runnable runnable) {
    if (mInstance == null) …
Run Code Online (Sandbox Code Playgroud)

android android-bitmap android-thread

1
推荐指数
1
解决办法
6772
查看次数

使用 BitmapFactory.Options();

我已经采取并修改了一些代码,用于获得较大(500x500)位图图像的高质量抗锯齿小版本。显然,使用 Matrix 比使用 createScaledBitmap() 产生更高的质量。结果不是很令人印象深刻..我怀疑也许我犯了一个错误,特别是我不确定是否真的使用了期权。所以我将其更改inSampleSize = 1;inSampleSize = 50;期望看到质量急剧下降,但没有任何变化。所以现在我怀疑选项被忽略了。这段代码能救出来吗?

我希望也许能找到某个版本的 createBitmap,它接受了 abitmap和一个options参数,但找不到。

BitmapFactory.Options options = new BitmapFactory.Options();

options.inPreferQualityOverSpeed = true;
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = 1;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap bmpSource = BitmapFactory.decodeResource(getResources(), R.drawable.bigicon);

Matrix matrix = new Matrix();
matrix.postScale(.1f,.1f);
Bitmap scaledBitmap = Bitmap.createBitmap(bmpSource, 0, 0, bmpSource.getWidth(), bmpSource.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud)

android bitmapfactory android-bitmap

0
推荐指数
1
解决办法
4778
查看次数

如何将画布转换为位图以保存在 sdCard 中

我使用 SurfaceView 在屏幕上移动两个位图图片。我试过这个:

...
@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);

  ...

  canvas.drawBitmap(bitmap,matrix,paint);
  canvas.drawBitmap(bitmap2,matrix,paint);
}
...
Run Code Online (Sandbox Code Playgroud)

如何像这样将画布保存到 sdCard 中?

public saveCanvasIntoSdCard(Canvas canvas)
{

}
Run Code Online (Sandbox Code Playgroud)

android surfaceview surfaceholder android-canvas android-bitmap

0
推荐指数
1
解决办法
3204
查看次数

如何在Android中调整位图图像的大小

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="32dp"
    android:paddingTop="10dp"
    android:paddingRight="0dp"
    android:paddingBottom="10dp">
    <item android:bottom="3dp">
        <bitmap
            android:gravity="center_vertical|left"
            android:src="@drawable/ic_publish_black_24dp" />
        <shape android:shape="rectangle">
            <stroke
                android:width="0dp"
                android:color="#37474f" />
        </shape>
    </item>
</layer-list>  
Run Code Online (Sandbox Code Playgroud)

这就是我所说的:

TextView aboutBags = new TextView( mContext );
aboutBags.setText("Compose");
aboutBags.setTextAppearance( R.style.RevStyleHelpAbout );
aboutBags.setBackgroundResource( R.drawable.rev_item_h_border_left );  
Run Code Online (Sandbox Code Playgroud)

我已经尝试android:width="22dp" android:height="22dp"过遍及XML的border属性,但是图像的大小没有调整。

如何调整位图图像的大小?

Vielen dank im voraus。

java android textview android-bitmap

0
推荐指数
1
解决办法
5927
查看次数

onActivityResult不显示图像

因此,我想拍照并显示在屏幕上。我可以按按钮并拍照,但屏幕上没有显示ImageView。我无法通过搜索找到解决方案onActivityresult

我现在所拥有的:

 public class MainActivity extends AppCompatActivity {


    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnCamera = findViewById(R.id.btnCamera);
        imageView = findViewById(R.id.imageView);


        // Open camera and take picture
        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);

            }
        });

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bitmap = (Bitmap)data.getExtras().get("Data");
        imageView.setImageBitmap(bitmap);
    }
}
Run Code Online (Sandbox Code Playgroud)

android android-bitmap

0
推荐指数
1
解决办法
88
查看次数

Android Studio 1.4.1中的运行时错误'IndexOutOfBoundsException'

我正在尝试关注精灵动画的youtube教程并且一直遇到通常与之相关的错误activity.这是一个:

java.lang.RuntimeException:无法启动活动ComponentInfo {rocketanimalcontrol.reap/rocketanimalcontrol.reap.MainActivity}:java.lang.IndexOutOfBoundsException:索引0无效,大小为0

罪魁祸首是在下面的某个地方吗?

MainActivity.java

package rocketanimalcontrol.reap;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity; //? This needed with AppCompatActivity ?
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.view.View;
import android.view.Menu;
//import android.view.animation.Animation;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ArrayList<Bitmap> bitmaps;//new here or in OnCreate?
    private SpriteAnimation animation;
    private CustomView view;

    //Auto-generated code
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view …
Run Code Online (Sandbox Code Playgroud)

java android runtime indexoutofboundsexception android-bitmap

-1
推荐指数
1
解决办法
194
查看次数

java.lang.OutOfMemoryError - How do I fix this?

I am trying to implement paging in one activity but I keep running out of memory. The app runs till the 4th page but when it gets to the 5th it crashes. I've tried increasing the JVM heap size but the app still crashes at this one point where it runs out of memory to allocate.

This is my main file:

public class S1Intro extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_s1);

    MyPagerAdapter adapter = new MyPagerAdapter(); …
Run Code Online (Sandbox Code Playgroud)

java android out-of-memory android-imageview android-bitmap

-1
推荐指数
2
解决办法
1168
查看次数